Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one thread access a synchronized non-static method and another thread access a synchronized static method at the same time?

Tags:

java

I got this asked in an interview a few days ago. Can one thread access a synchronized non-static method and another thread access a synchronized static method at the same time? The methods belong to the same class. I know the answer is yes but I want to know how it is possible. Thanks.

like image 297
Zim Avatar asked Dec 13 '25 23:12

Zim


1 Answers

The synchronization object for a non-static method is the object itself (this).

The synchronization object for a static method is the .class instance.

Both are different. Hence you can.

like image 130
A.H. Avatar answered Dec 16 '25 13:12

A.H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!