Does synchronized on methods make other threads that try to access that method wait until it is executed?

StackOverflow https://stackoverflow.com/questions/22429577

Question

I just wanted to know if Thread1 adds elements to a list with a synchronized method and Thread2 tries to add elements to the same list does Thread2 wait until Thread1 has finished adding its elements or does it just check and if it can't access the method then it doesn't even bother trying to add elements to the list anymore?

Thank you!

Était-ce utile?

La solution

Thread2 will wait.

I suggest you to read the official documentation about this topic it is rather thorough.

If you do not want to worry about this problem try the data structures in the java concurrent packages.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top