Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, can thread switching happen in the synchronized block?

I understand a synchronized block or method will block all threads until the one inside has "left". I am wondering, can context switching happen when thread is executing inside synchronized block? In my understanding, it shouldn't.

Thanks!

like image 392
Abidi Avatar asked Nov 23 '13 13:11

Abidi


1 Answers

can context switching happen when thread is executing inside synchronized block?

Yes, a context switch can happen inside a synchronized block as well. The only thing that's going to be different is that no other thread would be able to enter the same synchronized block (or any other block synchronized on the same object) until the context switches back to that pre-empted thread, letting it finish with its protected code.

like image 106
Sergey Kalinichenko Avatar answered Oct 27 '22 00:10

Sergey Kalinichenko