Suppose I have one method and inside that there is synchronization block is there. I have two thread on same object. One thread is executing sync block, so other thread will execute the rest of code of method.
1 Answer
While First thread executing in sync block, then second thread will execute all things before the blocks in the method and wait to enter the sync block. Whenever first thread exit from the sync block, then second thread will continue to enter the sync block.
4 Comments
anand patel
void disNum()// instead of method will take block { synchronized (this) { for(int i=65;i<75;i++) { System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } for(int j=0;j<5;j++) { System.out.println("Non synchronized-----"); } }
anand patel
As i have posted Q first thread enter to syn block but 2nd thread should execute that loop which is not part of syn block but its not happening. 2nd thread is waiting how?
Nur Zico
the loop is is not before the sync block. Everything before the sync block will execute by 2nd thread. Not after the block.
anand patel
understoood but as per rule that isn't part of syn block so it should execute parallel.means other thread can continue.