• 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

Interaction between threads

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
To understand the concept of wait() method in Object Class, i have written the following code.

Concept:wait causes the the thread to release the lock it is holding on an object.
[code]
class TestWait extends Thread{


public synchronized void run(){

for(int i=0;i<10;i++){
System.out.println("New Thread");
System.out.println("I"+" "+i);
}
notify();
System.out.println("After notify");
}


public void Start(){
synchronized(this){

try {
System.out.println("Before Wait");
this.wait();
System.out.println("After Wait");
}catch(InterruptedException e){
e.printStackTrace();
}

}
System.out.println("End");


}



public static void main(String[] args) {
TestWait testwait=new TestWait();
new TestWait().start();
testwait.Start();

}
}
[code]

There are two threads one -main,two-new thread.when the main thread relases the lock,the new thread continues and finishes its job.the problem is that after calling notify the main thread is not resuming and not finshing the remaining statements[("After Wait"),"End"].why?

Thanks & Regards
Kirba
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling Start and run() on two different objects. One thread is waiting on a different object and other is sending notification on a different object.
 
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


TestWait thread created in line X starts,runs thru for-loop,notifies and dies before main thread is blocked by testwait.Start() @ line Y. Hence,main thread is never notified and it waits eternally.

Have attached a program that can help you understand wait() n notify() better. This is what the program does:

* creates and starts 10 threads
* each thread blocks by waiting
* each thread is notified




Note that a thread may accidentally come out of wait() due to spurious wake-up. Hence,wait() should always be used following the standard idion:



Hope that helps.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sarvesh: TestWait thread created in line X starts,runs thru for-loop,notifies and dies before main thread is blocked by testwait.Start() @ line Y. Hence,main thread is never notified and it waits eternally.



Hey sarvesh, the problem here is not timing but the point that the wait and notify is done on different objects.
The synchronization is done on "this" and if you see this code:



The objects referred by "this" in Start() and run() will be different. So, no matter what the timing of wait call is, it will never get a notify.
 
sarvesh meens
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Kant : The objects referred by "this" in Start() and run() will be different. So, no matter what the timing of wait call is, it will never get a notify.



* Right,wait() and notify() are done on different objects and this is the fundamental problem.
* Even if,wait() and notify()are done on the same object,there is a possibility for timing issue here. To illustrate this point,updated and exeuted the program.



This is how the o/p looks:

main I'm gonna sleep
Thread-1 New Thread
Thread-1 I 0
Thread-1After notify
main oh oh! who woke me up?
main Before Wait

Though wait() and notify() are done on the same object, Thread-1 notifies before main gets to wait().
Hence,main thread will wait for eternity.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sarvesh: Even if,wait() and notify()are done on the same object,there is a possibility for timing issue here


Oh yeah absolutely, what i meant was that in the given program different monitor instances is the problem.
reply
    Bookmark Topic Watch Topic
  • New Topic