Thread 1 runs in java synchronized method 1, can Thread 2 execute java synchronized method 2?

I wonder if anyone can help me figure this out. (Student)

Let's say we have two threads: "Thread1" and "Thread2". If Thread1 is executed in method 1, can Thread2 execute in method2?

void method1() { synchronized (this) { } } void method2() { synchronized (this) { } } 

I either think that yes, Thread2 can come in, since "this" is just an instance of this method or not, because "this" is an instance of this class, and Thread1 rests on it.

+6
source share
6 answers

There is no monitor associated with a particular method β€” there is a monitor associated with the object. Therefore, if you try to synchronize the same object in both methods, the second thread will block until the first thread releases the monitor.

(Personally, I do not like synchronizing to this anyway - I am synchronizing a reference to an object that only my class has access to. But this is another matter.)

+9
source

Here, everyone answered with the obvious answer. This is the correct answer, but it is not the only correct answer.

The rule is that two threads cannot synchronize simultaneously on the same object. But does this refer to the same object in both method calls? We can’t say because you didn’t show us the calls. Consider this example:

 class Demo { synchronized void method1() { ...do something... } synchronized void method2() { ...do something else... } static void caseA() { final Demo demo = new Demo(); new Thread(new Runnable(){ @Override public void run() { demo.method1(); } }).start(); new Thread(new Runnable(){ @Override public void run() { demo.method2(); } }).start(); } static void caseB() { final Demo demo1 = new Demo(); final Demo demo2 = new Demo(); new Thread(new Runnable(){ @Override public void run() { demo1.method1(); } }).start(); new Thread(new Runnable(){ @Override public void run() { demo2.method2(); } }).start(); } } 

In case of A (), calls to demo.method1 () and demo.method2 () cannot overlap, because both calls are synchronized on the same object, but in case of B (), two calls are synchronized in two different instances of the Demo class. In case B (), the call to method1 () and the call to method2 () may overlap.

+4
source

You use this for synchronization, which is an instance of an object. Synchronization always works on a particular instance of an object, and only one thread at any given time can get a lock (synchronize) on one instance used for synchronization, and.

Both threads can access these methods only if they use separate instances of the object containing these methods.

+3
source

The second thread will block until the monitor is released first, since synchronization is performed on the same object for both methods.

+1
source

It is better to sync the static object that you want to lock. Here, your method 2 will be executed by thread 2, but the contents of the synchronized block in method 2 will not be executed if thread 1 does not unlock this . Since your synchronization is on the block, not on the method, the method is not synchronized, but there is a block.

If you have a static member on which you want to work in synchronized mode, use it as:

 private static List<Object> myList; void method1() { synchronized (myList) { // do something on myList } } void method2() { synchronized (myList) { // do something on myList } } 
+1
source

No Thread2 will have to wait until Thread1 completes code execution in the synchronized block. This is because you are using the same object (this one) to lock. If you did something like this:

 Object lock1 = new Object(); Object lock2 = new Object(); void method1() { synchronized (lock1) { } } void method2() { synchronized (lock2) { } } 

Then you can have Thread1 execute1 and Thread2 execute method2.

0
source

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


All Articles