Thread Behavior with a Combination of Synchronized Methods in a Class

Case 1

I have 2 synchronized methods as shown below:

class A { public void synchronized methodA() {} public void synchronized methodB() {} } 

A: I have streams T1 and T2. Can threads simultaneously execute methodA and methodB (respectively) belonging to the same instance of class A?

  • My analysis: the answer is no , because only one method will be executed by thread T1, and thread T2 will be blocked until T1 completes execution.

B: I have flows T1 and T2. Can threads simultaneously execute methodA and methodB (respectively) belonging to different instances of class A?

  • My analysis: the answer is yes , since T1 and T2 can execute methodA and methodB belonging to different instances of class A and they will not be blocked.

As far as I understand my understanding in accordance with my analysis for case 1?

Update: Case 2

I have 2 synchronized methods, one is not static and the other is static.

 class A { public void synchronized methodA() {} public void static synchronized methodB() {} } 

A: I have streams T1 and T2. Can threads simultaneously execute methodA and methodB (respectively) belonging to the same instance of class A?

  • My analysis: the answer is no , because only one method will be executed by T1 and T2 will be blocked until T1 completes the execution.

B: I have flows T1 and T2. Can threads simultaneously execute methodA and methodB (respectively) belonging to different instances of class A?

  • My analysis: the answer is yes , because T1 and T2 can execute methodA and methodB belonging to different instances and they will not be blocked.

As far as I understand, according to my analysis for case 2?

+4
source share
2 answers

Case A: No

Case B: Yes

Your analysis is correct. The synchronized keyword in the instance method is functionally equivalent to this:

 public void someMethod() { synchronized(this) { //code } } 

Since this differs in two contexts, they are not mutually exclusive. Note that even the same method could be called simultaneously for two different instances.

Edit

For case 2, you are wrong on 2A; it doesn't even make sense. The static synchronized method does not "belong" to the class instance; if it were done, it would not be static! It does not synchronize with any instance of the class (it does not have a reference to the instance to synchronize to!), So instead it synchronizes an object of class A The static synchronized method in class A equivalent to this:

 public static void methodB() { synchronized (A.class) { //code } } 

Your instance method will synchronize in the instance as shown above, so two threads can execute in parallel.

In case B then, obviously, they can also work in parallel, but you still do not understand the fundamental concept that the static method is not associated with any instance.

+4
source

Case 1: You are right.

Each Java object has (among other things) a mutex lock. When synchronized is applied to a non-stationary method, it is the mutex of the instance that is locked. Thus, synchronized methods work on the basis of each object.

The synchronized static methods use the locking of the Class object; therefore, they work on the basis of the class (modulo ClassLoader) and independently of non-static methods. You can also use synchronized() in method bodies to use the lock on any object you want.

Case 2: You are wrong because the methods A () and methodB () use different mutexes, as described above.

0
source

Source: https://habr.com/ru/post/1332769/


All Articles