Java Programming - Threads - Discussion


Which of the following will directly stop the execution of a Thread?

[A]. wait()
[B]. notify()
[C]. notifyall()
[D]. exits synchronized code

Answer: Option A


Option A is correct. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

Option B is wrong. notify() - wakes up a single thread that is waiting on this object's monitor.

Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor.

Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Does entering/exiting synchronized code mean that the thread execution stops? Not necessarily because the thread can still run code that is not synchronized. I think the word directly in the question gives us a clue. Exiting synchronized code does not directly stop the execution of a thread.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.