Java Programming - Threads - Discussion

Discussion Forum : Threads - General Questions (Q.No. 5)
5.
Which cannot directly cause a thread to stop executing?
Calling the SetPriority() method on a Thread object.
Calling the wait() method on an object.
Calling notify() method on an object.
Calling read() method on an InputStream object.
Answer: Option
Explanation:

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

Discussion:
6 comments Page 1 of 1.

Deva said:   1 decade ago
Can anyone explain the answer briefly???

Jailalita said:   1 decade ago
notify() method is used to activate only one thread.

Roop Chaudhary said:   1 decade ago
(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:   8 years ago
The notify() is used to activate only one thread.

Priya said:   8 years ago
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:   8 years ago
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:

Your comments will be displayed after verification.