Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Doing something when a thread finishes running

Thread view: 
Nicky Chorley - 28 Aug 2007 08:24 GMT
Hi,

I have a program that currently uses two threads - the main thread
that just sets up a GUI, adds ActionListeners to buttons, etc and
another thread that does some processing and updates the GUI. The
processing thread reads lines from a file (amongst other things) and
terminates when the end of the file has been reached (I assume it's
terminating anyway. There's an infinite while loop in my Runnable's
run() method and I break out of it at end of file). While doing the
processing, this thread stores results in an object (in this case a
vector) and what I would like to do is display another window with
results in right after the processing thread has finished. I'm
thinking this involves doing a notify() somewhere but am stuck working
out how. Any hints?

Regards,

NC
Duncan - 28 Aug 2007 10:40 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> NC

Hi NC,

I think one option is to use the "join()" method of the processing
thread.  You could create another thread to display this window and
call <processing thread>.join().  This instructs your new thread to
wait until the processing thread has exited.  If you place your code
to display the window in this new thread, directly after the call to
join(), it will only run once the processing thread has finished.

Of course, you will need to ensure this new thread has access to the
object the processing thread is using!

Hope this helps,

Duncan Jones
Lew - 28 Aug 2007 13:51 GMT
>> Hi,
>>
[quoted text clipped - 23 lines]
> to display the window in this new thread, directly after the call to
> join(), it will only run once the processing thread has finished.

Executor, Future<V>, FutureTask<V> and related interfaces and classes.
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/Executor.html>
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html>
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/FutureTask.html>

You get the result with Future.get().  There are ways of capturing the
exceptions the task might have thrown, too.

/Java Concurrency in Practice/ by Brian Goetz (with Peherls, Bloch and others)
covers idioms for these uses.

Signature

Lew

Nigel Wade - 28 Aug 2007 14:32 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> NC

If you are using Java 1.6 then look at SwingWorker.
I think that class will do everything you require. In particular look at the
done() method, which is executed on the EDT, after the doInBackground() method
completes.

http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Nicky Chorley - 28 Aug 2007 16:03 GMT
Thanks for the suggestions all, I will look at them and see which is
appropriate!

Nicky Chorley
Roedy Green - 28 Aug 2007 23:46 GMT
The easiest way to do it is to have the terminating thread do the
work, or if it it is Swing work, schedule a run packet with
SwingUtilities.invokeLater.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Nicky Chorley - 02 Sep 2007 17:26 GMT
Hi again,

I found the simplest was was just to create and display my new window
in my processing thread. Don't know why I didn't think of this myself!

Thanks again,

NC


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.