“new”中/后带有大括号的代码?

发布于 2024-11-19 22:49:32 字数 264 浏览 5 评论 0原文

private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>() {
    public Connection initialValue() { 
         return DriverManager.getConnection(DB_URL);
    }
};

我不明白星星内部发生了什么。这是将方法插入类的方法吗?

private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>() {
    public Connection initialValue() { 
         return DriverManager.getConnection(DB_URL);
    }
};

I don't understand what's going on in the part that's within the stars. Is that a way to insert a method into a class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝眼泪 2024-11-26 22:49:33

initialValue() 方法只是构造一个持有非 null 值的 ThreadLocal 的方法。

编辑:哦,我明白这不是你问的问题。你所拥有的与你所做的一样:

public class MyOwnThreadLocal extends ThreadLocal {
    public Connection initialValue() {
        return DriverManager.getConnection(DB_URL);
    }
}

除了你的版本不需要完全独立的类定义 - 因此它被称为“匿名类”。

The initialValue() method of ThreadLocal is just a way to construct a ThreadLocal holding a value other than null.

Edit: Oh, I see that's not what you're asking about. What you have there is the same as if you did:

public class MyOwnThreadLocal extends ThreadLocal {
    public Connection initialValue() {
        return DriverManager.getConnection(DB_URL);
    }
}

Except your version doesn't require a completely separate class definition--hence it's called an "anonymous class".

年少掌心 2024-11-26 22:49:33

在星星中,您有一个匿名内部类,这意味着:

  • 您正在将一个类嵌入到一个类中(因此它是一个内部类),
  • 该内部类是在方法体中定义的(因此它是一个匿名内部类),

它是实现的语法快捷方式接口或抽象类的行为,无需声明完整的标准 Java 类。您正在为固定上下文定义精确的行为。

接下来,对于ThreadLocal部分,基于ThreadLocal Javadoc中connectionHolder字段是由很多thead管理的。

只要线程处于活动状态并且 ThreadLocal 实例可访问,每个线程都会保留对其线程局部变量副本的隐式引用;线程消失后,其线程本地实例的所有副本都将受到垃圾回收(除非存在对这些副本的其他引用)。

如果你有 5 个线程访问 ConnectionHolder,你会发现 5 个 ThreadLocal 管理着该值的 5 个实例

Within the stars you have an anonymous inner class it means :

  • you are embedding a class in a class (so it's an inner class)
  • the inner class is defined within a method body (so it's an anonymous inner class)

it's a syntax shortcut to implement a behavior for an interface or abstract class without declaring a full standard Java class. You are defining a precise behavior for a fixed context.

Next, for the ThreadLocal part,based on the ThreadLocal Javadoc the connectionHolder field is managed by many theads.

Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

If you have 5 threads wich access to the connectionHolder you will found 5 ThreadLocal which managed the 5 instance of the value

在你怀里撒娇 2024-11-26 22:49:33

这是一个匿名内部类,通常实现一些接口是回调或类似的东西,Swing 使用它作为侦听器。

在本例中,它在 ThreadLocal 的特定实例上实现 initialValue() 方法。

您可以通过这种方式重写对象的单个实例上的方法,因此,如果您只想某个特定实例以不同的方式运行,您可以重写该实例上任意数量的方法。

This is an Anonymous Inner Class that are usually implementing some interface that is a callback or something similar, Swing uses this for listeners.

In this case it is implementing the initialValue() method on that particular instance of ThreadLocal<Connection>.

You can override methods on single instances of Objects this way, so if you want just a particular instance to behave in a different way you can override as many methods on that instance you want.

滥情稳全场 2024-11-26 22:49:33

这是一个匿名内部类。这是扩展基类的简短语法:

class AnonClass extends ThreadLocal<Connector> {
    public Connection initialValue() { 
         return DriverManager.getConnection(DB_URL);
    }
}

私有静态 AnonClass 连接持有者 = new AnonClass();

您可以使用接口执行相同的操作(最常见于实现回调时)。例如:

interface Callback {
    void doIt();
}

回调测试 = new Callback() { 公共无效 doIt() { /* ... */ } };

This is an anonymous inner class. It is a short syntax for extending a base class:

class AnonClass extends ThreadLocal<Connector> {
    public Connection initialValue() { 
         return DriverManager.getConnection(DB_URL);
    }
}

private static AnonClass connectionHolder = new AnonClass();

You could do the same thing with interfaces (most commonly seen when implementing callbacks). For example:

interface Callback {
    void doIt();
}

Callback test = new Callback() { public void doIt() { /* ... */ } };

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文