0

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 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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-----"); } }
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?
the loop is is not before the sync block. Everything before the sync block will execute by 2nd thread. Not after the block.
understoood but as per rule that isn't part of syn block so it should execute parallel.means other thread can continue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.