File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() without notify() or notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() without notify() or notifyAll()" Watch "wait() without notify() or notifyAll()" New topic
Author

wait() without notify() or notifyAll()

Prabath Siriwardena
Greenhorn

Joined: Mar 27, 2004
Posts: 15
Dear Friends...!
What will happen if we use a wait() without having notify() call in the programme.
According to the theory the thread waiting should wait forever until another
thread notifies on that object, which the previous thread is waiting for .
But in the following programme I coudn't see any differenecs in the output
when I use wait() with and without notify();
Please tell me why ?
Here is the code...!!!
class Reader extends Thread {
Calculator c;
public Reader(Calculator calc) {
c = calc;
}
public void run() {
synchronized(c) {
try {
System.out.println("Waiting for calculation..." );
c.wait();
} catch (InterruptedException e) {System.out.println(e);}
}
System.out.println("Total is: " + c.total);
}

public static void main(String [] args) {
Calculator calculator = new Calculator();
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calculator.start();
}
}

class Calculator extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
// notify();
}


<a href="http://psiriwardena.blogspot.com/" target="_blank" rel="nofollow">http://psiriwardena.blogspot.com/</a><br />B.Sc(Eng),SCJP,SCWCD,SCBCD,SCDJWS,<br />MCSD,OCA,CCNA.BCS,ACS
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
This issue (wait() without notify()/notifyAll()) was raised earlier in another thread without a definitive answer...there was an interesting hypothesis proposed, though....


Rich
SCJP 1.4
Prabath Siriwardena
Greenhorn

Joined: Mar 27, 2004
Posts: 15
Dear Friend..!
A found a solution to the above , my own problem.
If you also had the same problem, elive this woud be useful.
Solution :
ThreadA, ThreadB and ThreadC are Reader Threads.
CalculatorThread and CalThread are Calculator Threads.
Case I
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notify() call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.

1)Only one Reader thread will be notified.(say ThreadB)
2)It will resume operations as soon as CalculatorThread notified and it exits the synchronized block, where “notify()” resides.
3)Other Reader threads will wait(). But now their wait() method behaves as the yield() method. ( Exactly as yield. It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
4)So as soon as CalculatorThread exits it’s run() method, ThreadA & ThreadC resume their operaions.

Case I I
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notify() ( or may not have a notify()) call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is not alive (dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
1)NO Reader thread will be notified.
2)All reader threads will wait(). But now their wait() method behaves as the sleep() method. ( Exactly as sleep(). It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)

Case III
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notify() ( or may not have a notify()) call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is not alive (dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
But CalThread is alive, and contains a notify() ( or may not have a notify())call inside it’s run() just after the expected task of Reader threads completed.
1)NO Reader thread will be notified, because none of them waits on CalThread object.
2)All reader threads will wait(). But now their wait() method behaves as the sleep() method. ( Exactly as sleep(). It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
Case I V
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notifyAll() call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.

1)All Reader threads will be notified.
2)All will resume operations as soon as CalculatorThread notified and it exits the synchronized block, where “notifyAll()” resides.
Case V
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has NO notify() or notifyAll() calls inside it’s run().
CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.

1)No Reader thread will be notified.
2)All Reader threads will wait(). But now their wait() method behaves as the yield() method. ( Exactly as yield. It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
3)So as soon as CalculatorThread exits it’s run() method, all Reader threads resume their operations.
 
 
subject: wait() without notify() or notifyAll()
 
Similar Threads
Question regarding notify() and wait()
wait() without notify() or notifyAll()
notify() and notifyAll()
synchronization doubt
Threading discrepancies