• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() without notify() or notifyAll()

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This issue (wait() without notify()/notifyAll()) was raised earlier in another thread without a definitive answer...there was an interesting hypothesis proposed, though....
 
Prabath Siriwardena
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic