Simplification of synchronized block in Java

谁说我不能喝 提交于 2019-12-08 13:09:16

问题


I'm having some trouble wrapping my head around the concept of synchronized blocks in Java. I feel I have understood synchronized methods well enough. So I thought of an analogy to help me understand synchronized blocks in terms of synchronized methods. Please let me know if this equivalence that I have proposed is correct. Also, I have only mentioned this for a non-static synchronized block for now. However, points on static synchronzied blocks are also welcome.

public void method() 
{
    //code snipppet A

synchronized(objRef) 
{ 
    //code snipppet B
}
    //code snipppet C
}

Is equivalent to

public void method() {
//code snippet A
objRef.sync_method();
//code snippet C
}

In the class of objRef:

public synchronized void sync_method() {
//code snippet B
}

This analogy is based on the logic that synchronized blocks behave just as synchronized methods do. Meaning that once a thread acquires a lock on the monitor, it does not allow other threads to interfere in its execution. The thread relinquishes control only once it has finished executing the entire synchronized method.


回答1:


You should notice that there are two types of synchronized block in Java (object synchronize and class synchronize). There are two synchronized methods (static and non-static methods) and their equivalent synchronized blocks (Class and Object Synchronized blocks respectively). assume we have:

class MyClass{
public synchronized static void syncStaticMethod(){...}
public synchronized void syncNonStaticMethod(){...}
}


MyClass objRef = new MyClass();

1. Object Synchronization:

public void method() {synchronized(objRef) { ... }}

Is equivalent to

 public void method() {objRef.syncNonStaticMethod();}

2. Class Synchronization:

 public void method() {synchronized(MyClass.class) { ... }}

Is equivalent to

   public void method() {MyClass.syncStaticMethod();}



回答2:


There is a possibility that you're not getting everything right. For example,

synchronized(objRef) {
    objRef.nonsync_method();
    ... more code ...
}

is not equivalent to

objRef.sync_method();

because in the former case the lock covers a larger part of the code. Equivalence exists between

synchronized void sync_method() {
    // stuff
}

and

void nonsync_method() {
    synchronized (this) {
        // stuff
    }
}

Further,

once a thread acquires a lock on the monitor, it does not allow other threads to interfere in its execution

The above may or may not be true, depending on how we interpret the ambiguous wording. A thread that has acquired a lock may easily allow other threads to interfere with its execution: all it takes is another piece of code not covered by exactly the same lock. For example,

int i;

synchronized void sync_method() {
    i = 0;
    System.out.println(i);
}

void nonsync_method() {
    i = 42;
}

The call to sync_method() can print both 0 and 42.



来源:https://stackoverflow.com/questions/40329531/simplification-of-synchronized-block-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!