wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes How can a thread wait on itself? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How can a thread wait on itself?" Watch "How can a thread wait on itself?" New topic
Author

How can a thread wait on itself?

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hello,

I'm not sure how this can be done.

This is what I want to do:


What I want to do is for the main to read the user input value 'after' the user has actually input it. But when I do T.start(), it starts the thread and then immediately reads the value before user inputs it.

I want some way to wait till the user inputs it. So I did join() in main method, and I wait() in run() method and finally when the user is finished input it notify() so the main method will the value 'after' the user has input it.

But this doesn;t seem to work.
I even tried 'this' instead of 'T' but with no luck.
When I do T.sleep(1000) it works. But the GUI is not complete. I get the buttons after the 1000ms is over. But wait() and notify() doesnt seems to work.

Thank you.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
Not sure what your sample code is meant to show. It's nothing workable.
Here's a simple example of join that waits until the thread exits to continue:
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you Norm.

The scheme would work, but is there anyway I can get rid of the sleep(). Because I have to specify a time and I need to get the user to input the value within that time.
Is there anyway I can do wait()/notify() ?

Actually the GUI presented in the thread has buttons and the textarea. The user either clicks a button or enters a text. So I have an actionlistener method.

What I really want to do is, make the thread present the GUI and then wait(). AFter the user has clicked button/ entered text, the actionlistener method will be called. After the necessary action is taken, the actionlistener method will call notify() and the thread will run to finish.
Then the main method can continue because the user input is available.

So basically, the main method starts the thread and calls join(), so it will wait until the thread is finish. The thread in turn will call wait(), and this will make it wait until the user is finished input values. The GUI actionListener will call notify(), this will break the wait() of the thread and the call will go back to main method, which is waiting on join(). When the main method gets out of join(), the user data is available so it can do the processing.

Anyway to do that?

THanks a lot.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
I am beginning to think this thread is going outwith the range of a beginner's question, and it ought to be moved to the Threads forum . . .
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
I put the sleep in as a way to simulate waiting on a user. It was NOT intended to be used in a real app. Since your code didn't have any GUI in it, I simulated waiting by sleeping

I don't think that using join() is the right way. I think wait() and notify would work better.

Here's the same code rewritten to use wait/notify:
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks a million Norm!
I have been using wait/notify incorrectly.
Will try this out. Thanks again mate.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Dear Norm,

There are few statements I am unclear about. I think I'm a little confused about 'synchronized statements'.

Why do I need a 'mon' object? is it like a dummy object to wait on?
And why do I need to put wait()/notify() within synchronized statements? What will happen if I do not? There will be only 'one' thread (T) that will notify 'mon'. So why do I need to synchronize it?

Thanks heaps mate.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Maduranga:
And why do I need to put wait()/notify() within synchronized statements? What will happen if I do not? There will be only 'one' thread (T) that will notify 'mon'. So why do I need to synchronize it?


Wait and notify must always be inside synchronize block.(synchronized on the same object on which the wait-notify is called.)
This post will explain the reason.


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can a thread wait on itself?