6 Ekim 2015 Salı

Do synchnorized blocks or methods on Java prevent from multi thread access for every thread?

Hi,

As you read the question, I want to answer it quickly :)

Answer is, it depends. If the synchronized method is marked as "static", yes, it will work for every instances of class, because static methods belong to class, not instance.
For synchronized blocks, if they are in a static method, the same things go for them.

Otherwise, if the method is not static, it will work per instance, one's lock will not affect the other instance's synchronized block.

Have a nice day!

Hiç yorum yok:

Yorum Gönder