Hi, I want to ask the following: If we have a method e.g.
public void doSomething(){
synchronized(this){
//some code processing here
}
String temp = "init"; //instead of i++
synchronized(this){
//some other code processing here
}
}
is this method equivalent to public synchronized void doSomething()? I mean, is there any reason not to assume that the thread scheduler in some executions would not result in effectively the same flow as synchronizing the whole function? I.e. Thread1 enters the first synchronized block, Thread2 blocks, Thread1 continues with i++ and moves to the second synchronized block while Thread2 remains blocked. As a result thread2 enters the method after thread1 has exited both synchronized blocks All I need to know is whether I can count on all execution contexts that both threads (e.g. Thread1 and Thread2) can be in the method at the same time, e.g thread2 in the first sync block and thread1 in the second sync block to achieve concurrency, or there will be some execution flows where only 1 thread will be in the method effectively serializing the whole flow making equivalent to public synchronized void doSomething()
Thanks