Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes How to tell one thread that another thread has finished Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to tell one thread that another thread has finished" Watch "How to tell one thread that another thread has finished" New topic
Author

How to tell one thread that another thread has finished

Peter Mayer
Greenhorn

Joined: Sep 01, 2007
Posts: 4
Hey!

I have a GUI which runs in the "main" thread and another thread fetching some data from a webservice. The data should then get displayed in the GUI. The fetch operation may take 20 seconds or longer. In the GUI have 2 buttons Cancel and Fetch. When clicking the "Fetch" button the thread which gets the data from the web service starts and the button gets disabled (setEnabled (false)). If the thread takes too long the user may click the "Cancel" button and the "fetcher" thread stops and the "Fetch" button gets enabled again. So my problem is how can I know when the "fetcher" thread is ready so that I can pass the data to the GUI and set the Fetch button enabled?

I first thought of using fetcher.join(); however this doesn't make sense as I then could use one thread as well. The same applies to wait and notify.

So is there a way to do this? To tell my main thread that my "fetcher" thread has finished and that he can get the data from the fetcher thread now?

Thanks a lot!

-- Peter
[ September 01, 2007: Message edited by: Peter Mayer ]
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
Originally posted by Peter Mayer:
Hey!

I have a GUI which runs in the "main" thread and another thread fetching some data from a webservice. The data should then get displayed in the GUI. The fetch operation may take 20 seconds or longer. In the GUI have 2 buttons Cancel and Fetch. When clicking the "Fetch" button the thread which gets the data from the web service starts and the button gets disabled (setEnabled (false)). If the thread takes too long the user may click the "Cancel" button and the "fetcher" thread stops and the "Fetch" button gets enabled again. So my problem is how can I know when the "fetcher" thread is ready so that I can pass the data to the GUI and set the Fetch button enabled?

I first thought of using fetcher.join(); however this doesn't make sense as I then could use one thread as well. The same applies to wait and notify.

So is there a way to do this? To tell my main thread that my "fetcher" thread has finished and that he can get the data from the fetcher thread now?

Thanks a lot!

-- Peter

[ September 01, 2007: Message edited by: Peter Mayer ]


How about passing the main gui instance reference as a parameter when creating the fetcher thread?

The fetcher can then execute calls to the main thread when needed.
Peter Mayer
Greenhorn

Joined: Sep 01, 2007
Posts: 4
Thanks for replying!

Of course this would work, however this would make unit testing more difficult and it creates a new dependency...

Does anybody know another way to do it?

Thanks!

Peter
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

First of all, the GUI doesn't run in the "main" thread. It may have been initialized and started by the main thread, but after it has been started, all AWT/Swing events are dispatched to the event handling thread. This means that the "main" thread may be off doing something else, etc.

Of course this would work, however this would make unit testing more difficult and it creates a new dependency...

Does anybody know another way to do it?


Well, there is already a (logic) dependancy, you just haven't coded it yet. Your two choices are... (1) the fetcher thread has a reference to the GUI instance so that it can (use SwingUtilites to) set the button or (2) the event dispatching thread has a reference to the fetch data, so that it can setup a Swing Timer to poll competion.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can lessen the pain of the dependency by turning it around. Make the worker package own an interface which is implemented by anyone who wants to know when the work is done. The GUI implements that interface and passes itself to the work, who only expects the interface. The worker thread does not depend on the GUI, but the GUI depends on the worker package twice.

At least we have all the arrows going one direction, and now you can fake up listeners for testing the "work is done" message.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peter Mayer
Greenhorn

Joined: Sep 01, 2007
Posts: 4
Thanks for replying!!

You can lessen the pain of the dependency by turning it around. Make the worker package own an interface which is implemented by anyone who wants to know when the work is done. The GUI implements that interface and passes itself to the work, who only expects the interface. The worker thread does not depend on the GUI, but the GUI depends on the worker package twice.

Good idea!

... and now you can fake up listeners for testing the "work is done" message.

Could you explain what you mean with that? I can't really follow you.

Thanks!

Peter
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can make a special implementation of TheInterfaceThatWantsToKnowWhenWorkIsDone just for testing so you can test without the GUI. Maybe that wasn't the kind of thing you meant when you mentioned testing earlier, so it might not be of interest.
Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
Peter,

Perhaps you can make use of firePropertyChange. Let the GUI be a listener and when the data is captured by the second thread, it fires a property change listener to all its listeners. This way you can have more than one listeners and they can all update themselves.

Regards
Arun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to tell one thread that another thread has finished
 
Similar Threads
Diff. betn multithreading and multiprocessing
Threads/ActionListeners
Memory leak with MySQL JDBC driver
Terminating a infinite loop from within the program itself
Passing updated data back to a Main GUI and listening to button clicks