Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait()/notify() Beginners Question

 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Can we implements wait()/notify() in main thread without crating anyother
thread. is this true that you must have two thread to implement wait()/notify()?

and this can be:

1) main Thread/ and one you create in your program .
2) two thread other then you main app. thread.

I know! I am confuse that's why here guys.

Please help me.
thanks
Waiting for your reply.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not implement wait() and notify(). They are implemented as part of java.lang.Object and hence available in all classes.

You can use wait() and notify() in any class.

While it is legal to call wait() or notify() in a single-threaded application, it is unlikely to be useful. Worthwhile applications of these methods are likely to be multi-threaded applications.

There is nothing special about the "main" thread of your application. It is simply the first thread that runs. After that, it's like any other thread. It can even exit, before the application, if it wants to (the JVM will not shut down until all other non-daemon threads finish). Therefore, you can use wait() and notify() in any thread you like, including, but not limited to, the "main" thread.
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter,

Can you give one example in which we will use wait()/notify() in main thread.
without creating another thread.

Thanks peter for you prompted. hopeing for same again.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishwas bhatt:

Can you give one example in which we will use wait()/notify() in main thread.
without creating another thread.


The purpose of wait() is to wait for another thread to call notify(). The purpose of notify() is to tell another thread that's called wait() to wake up and resume operation.

Therefore, as Peter said, while nothing stops you from saying



in a single-threaded application, you'd never want to do this; the application would hang and stop functioning completely.
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks One again,

Lets suppose I have two classes A and B ok.


without creating thread can I do somethig where object of class A waits untill it notify from Class B object. If yes please explain with code..it will help me lot.

Thanks folks for all your help.

waiting for reply
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! If your single thread calls wait(), then no other object can do anything at all, so of course it can't call notify() -- unless, of course, there's another thread involved.

In a graphical program, main() runs in one thread, while event handlers run in a dedicated event thread, so in that case, main() could call wait(), and some event handler could notify() it. But that's no different from a multithreaded program where the multiple threads are created by you.
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest thanks....you hit the bullseye...this what i want to do..

Please take it in this way..

class a
{


}

class b implements Someeventlistener
{

keypress(){}



}

in class 'a' I am creating one form or i want to add three button to it.
But after adding first button class 'a' instance will go in wait status. And notify via class 'b' instance on keypress event and 'a' will add second button. is this possible?

If yes then please tell me how.
Thanks.
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Anybody there who can help me out in this.

thanks
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishwas bhatt:

But after adding first button class 'a' instance will go in wait status. And notify via class 'b' instance on keypress event and 'a' will add second button. is this possible?



I suppose, you should set out your problem and should not try to give an answer. Why do you think that you must use wait()/notify()?

Try to ask about you problem here:
Java in General
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic