Java Sync Blocks

If we have a method:

public void doSomething(){ synchronized(this){ //some code processing here } String temp = "init"; //instead of i++ synchronized(this){ //some other code processing here } } 

public synchronized void doSomething() this method equivalent to public synchronized void doSomething() ?

Is there any reason not to assume that the thread scheduler in some versions will not work effectively with the same thread as the synchronization of the whole function? I.e:

  • Thread1 enters the first synchronized block.
  • Thread2 Blocks.
  • Thread1 continues with i++ and goes into the second synchronized block while Thread2 remains locked.
  • As a result, Thread2 introduces the method after Thread1 exits both synchronized blocks.

All I need to know is:

  • Is it possible to count on all execution contexts that both threads (Thread1 and Thread2) can be in the method at the same time? For example, Thread2 in the first synchronization block and Thread1 in the second synchronization block to achieve concurrency.
  • Will there be any threads of execution in which only one thread in the method (in essence) will effectively serialize the whole thread, making it equivalent to public synchronized void doSomething() ?
+4
source share
6 answers

In some executions it will have the same thread as the synchronization of all functions, of course, but in order for it to be really equivalent to synchronizing this method, it should have the same thread for all executions.

As it is, there is a chance that another thread will capture the lock (whether for this method or some other code lock on one monitor) halfway through execution. This could not happen if the method itself was synchronized, so they are not equivalent.

(In any case, locking on this is considered bad practice anyway, I cannot remember the last time I wrote a synchronized method. Instead, I block private monitors so that I know my code, this is the only code that can block them.)

EDIT: To respond to your edit:

All I need to know is whether I can count on all execution contexts that both threads (for example, Thread1 and Thread2) can be in the method at the same time, for example, thread2 in the first synchronization block and thread1 in the second synchronization block for concurrency achievements

Absolutely not! It guaranteed that you would not have two streams as in the synchronized block synchronized on one monitor.

You have three sections of code: the first synchronized block, the unsynchronized part, and the second synchronized part.

Any number of threads can execute in the unsynchronized part at a time. For any instance (since you are synchronizing on this ), only one thread can execute any of the synchronized blocks. If you want to achieve concurrency, you will have to synchronize them on different monitors.

Also, it looks like you want the scheduler guarantees to allow another thread to capture the lock if it was waiting for it. I do not believe that there is such a guarantee - a thread executing the first block can release the lock, but continue in the same temporary sheet and re-acquire it before all other threads enter. In some JVMs that may not happen, but I don’t know, t believe there is no guarantee around it.

+9
source

No no. For example, for the code above

The thread included in the first sync'd block executes it and exits, then disconnects. The second thread enters the first sync'd block, which it increments, and then goes to the second synchronization block before shutting down. Stream one cannot now continue until the second stream exits the second sync'd block.

This pattern cannot happen if the whole method is synchronized.

+3
source

Given that the synchronized keyword is used to implement monitors in Java, it is impossible to guarantee that this piece of code will be synchronized.

In fact, is it possible for both the threads in question to complete the first synchronized block, and then execute the statement to increase the value of i until the next block is executed.

I assume that the variable i contains a state shared between two threads, in which case the operation is not thread safe. To make the sequence of operations thread safe, you must ensure that the entire sequence runs one thread at a time. Performing individual operations on individual monitors is as good as performing operations without a monitor; the entire sequence must be protected by the monitor.

More information on artima.com in the chapter example from inside the Java virtual machine .

EDIT

Given the fact that the question now reflects the use of a local String object, the sequence of operations can be considered thread-safe. Each thread creates its own local link on the stack for the String object; any mutation of an object in one thread will not affect the other due to the immutable property of String objects (a new String object is created for all practical purposes when a mutation occurs, and therefore the state is not really shared between threads).

Emphasis

When trying to synchronize threads, consider the sequence of operations that should be thread safe if access to shared data is made mutually exclusive; thus, one thread will not be able to read or write shared variables, and the other will perform an operation using shared data. Only using only local variables eliminates the sense of exchange between threads.

+2
source

No, this is not equivalent to synchronized void doSomething() , because i++ not an atomic operation. In fact, he probably does something like

int temp = i; i = i + 1; result = temp

If these operations are not atomic, then the value of i can be read when it is in a bad state.

+1
source

I need to know if I can count on all execution contexts that both methods (for example, Thread1 and Thread2) can be in the method at the same time, for example thread2 in the first synchronization block and thread1 in the second synchronization block to achieve concurrency

Not! It never happens. There is only one castle associated with this . By using the same lock for both synchronized blocks, it is not possible for Thread2 to be in the first synchronized block if Thread1 is in the second synchronized block.

All the other answers that the method you posted do not match the synchronization of the whole method are technically correct.

+1
source

This method is not like its synchronized method. The behavior that you explained can be seen, but can never be guaranteed.

0
source

All Articles