Synchronization does mean that all threads block waiting to get a lock before they can enter a synchronized block. Only one thread can have an object lock, so only one thread can be in the add() or sub() methods.
However, this does not mean anything else about streamlining. You start three threads - the only guarantee is that they will not stomp on each other, while running the add or sub methods. Topic 1 can call add() , then thread 3 can call add() , then thread 2 can call add() , then all of them can call sub() . Or they could all call add() , and then sub() each. Or any mix is ββthe only requirement that each thread calls add() before it calls sub() , and that neither of the two threads ever calls add() or sub() , while another thread is in this method.
In addition, in some cases, this may be a bad form for synchronizing with this , since it is publicly available - it often prefers to use a closed, closed Object to block, so that no other callers can take your lock and violate any blocking strategies that you have developed.
source share