How does a parent thread know when a child dies

R

rp

Hello,

I have a frame that when a user presses a button, i read a bunch of
files into a byte array and then push that byte array to a httpd
server.

the loading of the files is its own thread (that way the gui can
update a progess bar and refresh itself.

My question is:
When the loading the files into the byte array is finished, i want the
next thread to fire off and upload the files to the web server.
I can not seem to find the best way to monitor the first thread to
know when it finishes? There is the thread methods like join and wait,
but i want the frame thread to stay runnning

I hope i am making sense.

Thanks for anyhelp
 
A

Anthony Borla

rp said:
Hello,

I have a frame that when a user presses a button, i read
a bunch of files into a byte array and then push that byte
array to a httpd server.

the loading of the files is its own thread (that way the gui
can update a progess bar and refresh itself.

My question is:
When the loading the files into the byte array is finished,
i want the next thread to fire off and upload the files to
the web server. I can not seem to find the best way to
monitor the first thread to know when it finishes? There
is the thread methods like join and wait, but i want the
frame thread to stay runnning

I hope i am making sense.

Not being privy to details of your project I can only provide very general
answers:

* If you just want to know if a task has been completed,
then the use of shared flag variables is possible. A
typical scenario is to:

- Have a thread set / clear a flag on termination

- Have a 'controller' thread periodically check the value
of these flag(s), so would know a task was complete.
If many possible threads / tasks, flag arrays or collections
could be used

* Place relevant threads into the same ThreadGroup. A
'controller' thread would call 'enumerate' to get an
array of the currently running group threads, use
'getName' to identify specific thread(s), and 'isAlive' to
see if they are still active

The first approach is the 'classic' one, implementable in just about any
multi-threading language, while the second approach is quite Java-specific.
Note:

* Variations exist
* There may be better approaches to your problem using other
techiques

I hope this helps.

Anthony Borla
 
C

Christophe Vanfleteren

rp said:
Hello,

I have a frame that when a user presses a button, i read a bunch of
files into a byte array and then push that byte array to a httpd
server.

the loading of the files is its own thread (that way the gui can
update a progess bar and refresh itself.

My question is:
When the loading the files into the byte array is finished, i want the
next thread to fire off and upload the files to the web server.
I can not seem to find the best way to monitor the first thread to
know when it finishes? There is the thread methods like join and wait,
but i want the frame thread to stay runnning

I hope i am making sense.

Thanks for anyhelp

Just let the thread tell the Frame it's finished when it comes at the end of
the run() method.
 
X

xarax

rp said:
Hello,

I have a frame that when a user presses a button, i read a bunch of
files into a byte array and then push that byte array to a httpd
server.

the loading of the files is its own thread (that way the gui can
update a progess bar and refresh itself.

My question is:
When the loading the files into the byte array is finished, i want the
next thread to fire off and upload the files to the web server.
I can not seem to find the best way to monitor the first thread to
know when it finishes? There is the thread methods like join and wait,
but i want the frame thread to stay runnning

I hope i am making sense.

Thanks for anyhelp

Your loader thread must initiate the stimulus for
the next thread to do something. This is usually
accomplished with wait() and notify() on an object
reference. The loader thread calls notify() on the
object that the next thread is wait()'ing on. When
the next thread awakens, it knows that there is
data ready for processing.

Another approach is to use an event queue. Objects
are placed on the queue by one thread and pulled
off by another thread. The object itself defines
the work to do (usually by implementing the Runnable
interface). When the loader thread finishes, it
creates a Runnable object and appends it to the
event queue. The next thread is waiting patiently
on the event queue for the next element to appear.

As for the question in the topic title, the join()
method is used for waiting until the referenced
thread terminates.
 
J

John C. Bollinger

rp said:
Hello,

I have a frame that when a user presses a button, i read a bunch of
files into a byte array and then push that byte array to a httpd
server.

the loading of the files is its own thread (that way the gui can
update a progess bar and refresh itself.

My question is:
When the loading the files into the byte array is finished, i want the
next thread to fire off and upload the files to the web server.
I can not seem to find the best way to monitor the first thread to
know when it finishes? There is the thread methods like join and wait,
but i want the frame thread to stay runnning

If you have some two operations that you will always perform together in
the same sequence, then why put them in different threads? That
unnecessarilly complicates the situation. But if you do have a good
reason to do this, then one way to accomplish it would be with the help
of something like this:

class Signal {
boolean sent = false;

public synchronized void send() {
if (!this.isSent()) {
sent = true;
this.notifyAll();
}
}

public synchronized boolean isSent() {
return sent;
}

public synchronized void receive() throws InterruptedException {
if (!this.isSent()) {
this.wait();
}
}
}

class SignalingThread extends Thread {

private Signal finishedSignal;

public SignalingThread(Runnable toDo, Signal finished) {
super(toDo);
finishedSignal = finished;
}

public void run() {
try {
super.run();
} catch (RuntimeException re) {
finishedSignal.send();
throw re;
}
}
}

class DelayedThread extends Thread {
private Signal startSignal;

public DelayedThread(Runnable toDo, Signal start) {
super(toDo);
startSignal = start;
}

public void run() {
try {
startSignal.receive();
super.run();
} catch (InterruptedException ie) {
// do nothing special
}
}
}

You would use it something like this:

[...]

void myMethod() {
Signal synchSignal = new Signal();
Runnable first = new FirstRunnable();
Runnable second = new SecondRunnable();
Thread t1 = new SignalingThread(first, synchSignal);
Thread t2 = new DelayedThread(second, synchSignal);

t1.start();
t2.start();
}

[...]

----

Use of a Signal object decouples the two Thread subclasses from each
other, and also solves some tricky timing issues that otherwise could
cause trouble. The synchronization is implemented in the run() methods
in both Thread subclasses so that there is no delay in the thread that
starts them. There are a number of issues that you might want to worry
about, such as implementing timeouts, different handling of exceptions,
etc., but such things could be built on top of a mechanism like the above.


John Bollinger
(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top