If I just use synchronized, not the wait/notify methods, will it still be thread-safe?
What’s the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
synchronizedmakes a method / block accessible by only on thread at a time. So, yes, it’s thread-safe.The two concepts are combined, not mutually-exclusive. When you use
wait()you need to own the monitor on that object. So you need to havesynchronized(..)on it before that. Using.wait()makes the current thread stop until another thread calls.notify()on the object it waits on. This is an addition tosynchronized, which just ensures that only one thread will enter a block/method.