Java Programming - Threads - Discussion

5. 

Which cannot directly cause a thread to stop executing?

[A]. Calling the SetPriority() method on a Thread object.
[B]. Calling the wait() method on an object.
[C]. Calling notify() method on an object.
[D]. Calling read() method on an InputStream object.

Answer: Option C

Explanation:

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


Deva said: (Aug 13, 2010)  
Can anyone explain the answer briefly???

Jailalita said: (Apr 24, 2012)  
notify() method is used to activate only one thread.

Roop Chaudhary said: (Jul 2, 2012)  
(A) Wrong because setPriority() method interrupt the Thread to go directly on defined priority Thread.

(B) Wrong because wait() method tells Thread for wait till notify() method call.

(C) Correct because there is no directly affect of notify() on Thread. It only notify the Thread to start execution of code that was stop by wait() method.

(D) Wrong because read() method force Thread to stop and start the execution of IO Stream first.

Vineet said: (Jun 11, 2016)  
The notify() is used to activate only one thread.

Priya said: (Jul 21, 2016)  
But notify method will not stop the execution rather it will start the execution of a thread waiting. Not convinced on the answer. Please explain.

Sl_C_J said: (Dec 19, 2016)  
This example show how it does:

public class NotifyStopping implements Runnable
{

@Override
public void run()
{
int i = 0;
while (i <100000)
{
System.out.println("ok");
i++;
Integer I = new Integer(i);
synchronized (I) {
if (I.intValue() == 10 )
notify();
}
try
{
thread.sleep(500);
}
catch (InterruptedException e)
{
}
}
}

public static void main(String[] args)
{
tThread t = new thread(new NotifyStopping());
t.start();
try
{
thread.sleep(5000);
}
catch (InterruptedException e)
{
}
}
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.