Java Programming - Threads - Discussion
Discussion Forum : Threads - General Questions (Q.No. 5)
5.
Which cannot directly cause a thread to stop executing?
Answer: Option
Explanation:
Option C is correct. notify() - wakes up a single thread that is waiting on this object's monitor.
Discussion:
7 comments Page 1 of 1.
PapaJ said:
6 months ago
The notify() method can only be executed from the synchronised area so until and unless the thread doesn't leaves the synchronised area no other thread can get the lock.
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)
{
}
}
}
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)
{
}
}
}
Priya said:
9 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.
Vineet said:
9 years ago
The notify() 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.
(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.
(2)
Jailalita said:
1 decade ago
notify() method is used to activate only one thread.
Deva said:
1 decade ago
Can anyone explain the answer briefly???
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers