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 and notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait and notify" Watch "wait and notify" New topic
Author

wait and notify

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
1)The wait and notify should be on the same object. what happens when they are acting on different objects.
2)when the wait method is interrupted, if not, then whats the purpose of giving the InterruptedException in catch block.
3) what happens when the notify method is not given.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

1)The wait and notify should be on the same object. what happens when they are acting on different objects.

1) Wait method waits for the acquisition of the lock on that particular object until some other thread notifies it.
So if wait is waiting on an object and notification is happening for a different object, the wait will go on indefinitely until it gets interrupted. Notify will notify to any thread waiting on that object. Since no threads are waiting on the particular object, nothing takes place.


2)when the wait method is interrupted, if not, then whats the purpose of giving the InterruptedException in catch block.

When wait method is invoked, the thread goes on to wait state until some other thread notifies it. If some thread interrupts it, the thread comes back to normal thereby throwing an interrupted exception.
Look at the code below
:

In the above code u can see that there are two threads running.
1) Main thread
2) Thread object t1.

Now when t1 is run, it invokes wait method on the StringBuffer object. The main thread is also running side by side. Main thread calls interrupt on thread t1. And so the thread t1 is interrupted.


what happens when the notify method is not given.

If the thread is waiting on that object and if it finds no notify method, it goes on indefinitely until the thread is interrupted. Look at my previous code. Just remove the interrupt statement and check the output.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi

i had some doubts on ur code.

1)Usuall we will be passing the reference variable of the class implementing the runnable interface.But here we are passing the ref var of the class extending the Thread class, which is no needed.since the start method will be existing for the extended class.


2) in the below code i tried to implement the notify too.Here i created another thread by using the extended class.But still the Thread waiting for the object is keep on waiting though the notify method is called.

clear my doubts.


Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Hello Parameshwaran,
1) Here basically i am using a StringBuffer object which has to be shared by the threads. So when i create an instance of the CallinInterrupt class, i have a stringBuffer object created. If i create another instance of it, a new stringBuffer object gets created and for which running synchronized on that wouldnt be of any use. So for this reason i have created only one object and created more than one instance of Thread class using that same object.

2) In ur code u havent started ur thread by invoking a start() call. So ur thread is still not in the runnable state.
Here i have changed the code a little bit, have a look at it:



Tell me if u have any more problem on these.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi animesh sorry to disturb u again

i not able to follow the flow i am explain the flow which i understand.

1) the Thread t1 is created by passing the object of the class which extends the thread class and overridding the run() method.
2) once the run method is invoked the thread will have the lock of the object "sb" to access the synchronised method.
3)on issuing the wait method on the object it means the thread is added to the waiting list of the object "sb" expecting to have the object lock again.
4)Now at the same time the thread 2 i.e another instance will wait for the lock to have access synchronized code inside the run method.


this is the point i get confuse "who" will access the callRun() method which will acquire the object lock of the object "sb" and notifies all the THREAD waiting for its object lock...

My doubt is Who is the one to access the callRun() method.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

this is the point i get confuse "who" will access the callRun() method which will acquire the object lock of the object "sb" and notifies all the THREAD waiting for its object lock...


The code which i have written down u can see that as soon as Thread t1 goes to waiting state, it makes the boolean variable(b) to true, and now since a new Thread t2 also starts running, it will find the boolean value(b) as true and so it can call callRun() method.
Hope its clear now
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi animesh

i agree that once b is ste to true the thread will go for callRun() method.

but since call wait method is first encountered for any new thread on invoking the start method.


the first thread will invoke the callWait() method and get in to the wait mode.

at the same time the thread 2 which is created by the main thread will also encounter the line callWait() method and wait for the lock.

i am really sorry that i didn't get ur point of how thread2 will move to the next line leaving the callwait() line,

can u explain me.
 
wood burning stoves
 
subject: wait and notify