Vidyalelo
Java Programming · Q71

Threads

Programming · Java Programming · question 71

Q71

Which of the following will ensure the thread will be in running state?

A.
yield()
B.
notify()
C.
wait()
D.
Thread.killThread()
E.
None of these
Answer

Answer: Option E

Solution

Answer: Option E
Solution:
Let's understand this question about threads in Java!
The question asks us which of the given options will guarantee that a thread is in the running state.
Option A: yield()
yield() suggests to the Java Virtual Machine (JVM) that the current thread is willing to relinquish its current use of a processor.
The JVM is free to ignore this suggestion. It doesn't *guarantee* the thread will stay running or give up the processor; it just offers a hint.
Option B: notify()
notify() is used for inter-thread communication.
It wakes up a single thread that is waiting on the object's monitor.
It doesn't directly put a thread into the running state; it makes a waiting thread *eligible* to run, but it might have to wait its turn.
Option C: wait()
wait() causes the current thread to *relinquish* the lock and enter the waiting state.
The thread only becomes *eligible* to run again when another thread calls notify() or notifyAll() on the same object.
So, wait() definitely does *not* ensure the thread is in the running state; it does the opposite!
Option D: Thread.killThread()
There is no standard Thread.killThread() method in Java for directly killing a thread in a safe way.
This method is not a part of Java Thread API.
Option E: None of these
Since none of the options guarantees that a thread is in a running state by their use,
hence Option E is correct
Therefore, the answer is Option E: None of these.