• 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

Thread Interaction - wait()

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following program

class threada
{

}
 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry the program is this one

class threada
{
public static void main(String args[])
{
threadb b = new threadb();
b.start();
synchronised(b)
{
try
{
b.wait();
}
catch(Interrupted Exception e)
{};
}
}
}
class threadb extends Thread
{
public void run()
{
synchronised(this)
{
System.out.print("running");
notify();
}
}
}

Intitially main thread is running and wait is called on the object b now which thread will be waiting and how will the flow be?
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
there are two threads - the 'main' and the 'threadB' ( who ll print "running" ),
Once you call b.start() , main could finished : to prevent that, there is the wait().
What means that the main has to wait that b notify() before ending. You wrote synchronized(b) because main has to own a lock on b, when main waits it release his lock and has to wait for it to go further(in this case to end).
I hope it help you a bit,

arno
ps : read K&B books pg 718->729 ( about Threads Interaction )
 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so do u mean when a currently running thread invokes wait on any object the current thread waits until it gets notify message from the corresponding object on which it called notify?
i am really confused in this part
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lavanya Raguram:
so do u mean when a currently running thread invokes wait on any object the current thread waits until it gets notify message from the corresponding object on which it called notify?
i am really confused in this part



When the currently running thread invokes wait() on any object, it will release the lock(s) of the object (note: this lock may be grabbed multiple times), and then wait for notifications on that object. At this point, it is no longer the currently running thread.

At some point later, a different thread (which is the currently running thread) will call notify() on the same object -- which can wake up this object. (or it can wakeup some other thread also waiting on the same object)

Once this thread is awoken, it is still blocked -- as it still needs to reacquire the lock(s) that it had given up eariler. Once that is done, it will return from the wait() method.

Henry
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was also confused in the beginning, keep working on it...once you understand the thread's working mechanism, you ll understand the most programms ( from scjp ).
good work, hooah!!

arno
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic