简体   繁体   English

为什么我不能在Java中同步实例块?

[英]Why can't I synchronize instance block in java?

When I tried following 当我尝试跟随

public class Test {

    synchronized(this){   // compiler complains here
        System.out.println("instance block");
    }

    public static void main(String [] args){

    }

}

isn't synchronizing instance block is just like synchronizing block of statements ? 是不是同步实例块就像同步语句块?

Thanks, Bharat 谢谢,巴拉特

While you can synchronized(this) in an instance initialiser block or in a constructor it is always always pointless as the object will not be shared at this stage. 尽管您可以在实例初始化程序块或构造函数中synchronized(this)对象,但它总是毫无意义的,因为在此阶段将不共享对象。 ie it is only accessible to one thread. 即它只能由一个线程访问。

You can make an object available to more than one thread during the constructor, but this is generally considered a bad practice. 您可以在构造函数期间使一个对象可用于多个线程,但这通常被认为是不好的做法。

Why don't you synchronize inside: 您为什么不在内部同步:

public class Test {

    {
        synchronized(this) {
            System.out.println("instance block");
        }
    }

    public static void main(String [] args){

    }

}

isn't synchronizing instance block is just like synchronizing block of statements ? 是不是同步实例块就像同步语句块?

AFAIK, no, because it's not just a "block of statements" but an instance initializer. AFAIK,不,因为它不仅是“语句块”,而且是实例初始化器。 If you want the block execution to be synchronized, you can always synchronize on the this reference inside the initializer. 如果要同步块执行,则始终可以在初始化程序内的this引用上进行同步。 Also, I don't think you can synchronize on top-level blocks (method blocks have a special syntactical support for this as you already know). 另外,我认为您不能在顶级块上进行同步(如您所知,方法块对此具有特殊的语法支持)。

public class Test {

    // can't synchronize on a top-level block
    synchronized(this) {
    }

    {
        // OK
        synchronized(this) {
        }
    }

    // Methods have special syntactic support
    public synchronized void doIt() {
    }

    public void doIt() {
        // same as above
        synchronized(this) {
        }
    }
}

You are actually touching part of the language reasoning. 您实际上正在触及语言推理的一部分。 It's said that constructors (which initializer block belongs to) not need to be synchronized because the are always called from a single thread. 据说构造函数(初始化程序块所属的)不需要同步,因为总是从单个线程调用。 Another call would simply create another instance. 另一个调用只会创建另一个实例。

But since the constructor can actually leak resources to other instances it is allowed to use inner synchronized blocks to allow proper synchronization. 但是,由于构造函数实际上可能会将资源泄漏到其他实例,因此允许使用内部同步块来允许适当的同步。

Because there is no this in a static initializer block. 因为在静态初始化程序块中没有此功能。

That block gets executed when the class definition gets loaded, and not when an instance is created. 该块在加载类定义时执行,而不是在创建实例时执行。

Its not necessary to synchronize within an static init block as loading classes is handled by the jvm before you get control. 无需在静态init块中进行同步,因为在获得控制权之前,装入类由jvm处理。

In short, keep the block and remove synchronized(this) 简而言之,保持该块并删除同步(this)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM