There are 10 threads waiting for the lock of an object. How will you bring the 5th thread myThread out of the waiting state?
-
By calling notify(5)
-
By calling notifyAll()
-
By calling myThread.notify()
-
By calling notify(myThread)
-
None of these
None of the given methods work: notify() takes no arguments (wakes one random thread), notifyAll() wakes all threads, and notify() is called on the lock object, not on the thread itself. There's no standard Java API to wake a specific thread by rank from a wait set.
In Java, thread notification is done through Object's monitor methods: notify() and notifyAll(), both of which take NO arguments. There is no notify(int) and no notify(Thread) method anywhere in the Java API, so 'notify(5)' and 'notify(myThread)' (the marked answer) reference non-existent methods and won't even compile. Critically, Java provides NO mechanism to wake one specific chosen thread out of a wait set: notify() wakes an arbitrary single waiter and notifyAll() wakes them all, but you cannot target 'the 5th thread.' 'myThread.notify()' calls notify on myThread's own monitor (and would require holding that monitor); it does not select a waiter on the target lock. Since none of the concrete options can bring one specific waiting thread out of the wait state, the correct answer is 'None of these' (id 536044).