aspose file tools*
The moose likes Threads and Synchronization and the fly likes Interaction between threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Interaction between threads" Watch "Interaction between threads" New topic
Author

Interaction between threads

kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
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
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
sarvesh meens
Ranch Hand

Joined: Mar 31, 2006
Posts: 43



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

Joined: Feb 25, 2007
Posts: 1638

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

Joined: Mar 31, 2006
Posts: 43

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

Joined: Feb 25, 2007
Posts: 1638

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interaction between threads