wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes wait()/notify() Beginners Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait()/notify() Beginners Question" Watch "wait()/notify() Beginners Question" New topic
Author

wait()/notify() Beginners Question

vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Nov 30, 2000
Posts: 129
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

Joined: Nov 30, 2000
Posts: 129
Hi there,

Anybody there who can help me out in this.

thanks
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
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


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait()/notify() Beginners Question
 
Similar Threads
File writing synchronization
Thread Specific methods why in Object class ?
Misunderstanding with the wait() method.
Using wait() and notify()
How to make a thread wait and restart?