• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

wait and notify

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic