Doing something when a thread finishes running

N

Nicky Chorley

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
 
D

Duncan

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

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
 
L

Lew

Duncan said:
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.

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.
 
N

Nigel Wade

Nicky said:
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

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
 
N

Nicky Chorley

Thanks for the suggestions all, I will look at them and see which is
appropriate!

Nicky Chorley
 
R

Roedy Green

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.
 
N

Nicky Chorley

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top