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

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

Pergunta

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!

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top