简体   繁体   English

在Java中,“同步”是否等效于“同步(this)”?

[英]In Java, is `synchronized` equivalent to `synchronized (this)`?

I was looking through the source code for DatagramSocket and I found this: 我正在查看DatagramSocket的源代码,发现了这一点:

public void disconnect() {
    synchronized (this) {
        if (isClosed())
            return;
        if (connectState == ST_CONNECTED) {
            impl.disconnect ();
        }
        connectedAddress = null;
        connectedPort = -1;
        connectState = ST_NOT_CONNECTED;
    }
}

My understanding of synchronized methods is that they lock on this . 我对同步方法的理解是它们锁定了this So is the code equivalent to the following? 那么代码是否等效于以下代码?

public synchronized void disconnect() {
    if (isClosed())
        return;
    if (connectState == ST_CONNECTED) {
        impl.disconnect ();
    }
    connectedAddress = null;
    connectedPort = -1;
    connectState = ST_NOT_CONNECTED;
}

Is there a reason why the language designers chose not to use a synchronized method here? 语言设计者为什么在这里选择不使用同步方法?

Yes, the two code snippets are equivalent. 是的,这两个代码段是等效的。

We can only guess why whoever wrote this code chose the more verbose version. 我们只能猜测,为什么编写此代码的人选择了更详细的版本。 It could be someone's personal preference, or a historical artefact. 这可能是某人的个人喜好,或是历史文物。 For example, it might have previously been synchronized(someObject) or might have only covered part of the method, and whoever refactored the method didn't convert the synchronized section into a synchronized method. 例如,它可能先前已被synchronized(someObject)或者可能只覆盖了该方法的一部分,并且重构该方法的人并未将已synchronized部分转换为已synchronized方法。

Is there a reason why the language designers chose not to use a synchronized method here? 语言设计者为什么在这里选择不使用同步方法?

I don't know their minds, but in my mind, the first way is better. 我不了解他们的想法,但是在我看来,第一种方法更好。 I really dislike the phrase "synchronized method", because methods are not what we need to protect with synchronization. 我真的不喜欢“同步方法”一词,因为方法不是我们需要用同步保护的东西。

We need to protect data . 我们需要保护数据

The entire point of synchronized blocks, is that often, in order to advance the state of the program, one thread must create a temporary, invalid state. synchronized块的全部要点是,为了提高程序的状态,通常一个线程必须创建一个临时的无效状态。 With appropriate use of synchronized we can prevent any other thread from seeing the invalid state. 通过适当地使用synchronized我们可以防止任何其他线程看到无效状态。 It means synchronizing not just the block of code that is changing the state, but also, any other block of code that can examine the state. 这意味着不仅要同步更改状态的代码块,还要同步可以检查状态的任何其他代码块。


I believe that the idea for synchronized methods came from a paper published in the 1970s describing monitors . 我认为,同步方法的想法来自1970年代发表的描述监视器的论文 https://en.wikipedia.org/wiki/Monitor_%28synchronization%29 A monitor basically is an object whose methods are all atomic. https://en.wikipedia.org/wiki/Monitor_%28synchronization%29监视器基本上是一个对象,其方法都是原子的。 This was a useful abstraction when computer scientists were beginning to explore and formalize ways of thinking about parallel programming; 当计算机科学家开始探索和形式化关于并行编程的思维方式时,这是一个有用的抽象。 but in a lot of practical applications, the "monitor" idea is too strict: It's hard to make the most efficient use of a multiprocessor system using only monitors. 但是在许多实际应用中,“监视器”的概念太严格了:仅使用监视器就很难最有效地利用多处理器系统。

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

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