Thread - on terminate event

V

Vojta

Hello,

please could you give an advice concerning threads? I'm a Delphi programmer.
In Delphi there is OnTerminate event of thread object which gives me a
feedback that the thread has ended. Imagine you have a button in your
application. If you press the button it becomes disabled and a thread
starts. When the thread terminates it calls OnTerminate event handler in
which the button is enabled again. How can I do this in Java?

Thank you! Vojta
 
I

im

Hello,

please could you give an advice concerning threads? I'm a Delphi programmer.
In Delphi there is OnTerminate event of thread object which gives me a
feedback that the thread has ended. Imagine you have a button in your
application. If you press the button it becomes disabled and a thread
starts. When the thread terminates it calls OnTerminate event handler in
which the button is enabled again. How can I do this in Java?

You can implement your own notification mechanism by calling a method from
the thread before it exits, or you can use Thread.join() in a different
thread.

Imre
 
S

Stefan Schulz

Vojta said:
Hello,

please could you give an advice concerning threads? I'm a Delphi programmer.
In Delphi there is OnTerminate event of thread object which gives me a
feedback that the thread has ended. Imagine you have a button in your
application. If you press the button it becomes disabled and a thread
starts. When the thread terminates it calls OnTerminate event handler in
which the button is enabled again. How can I do this in Java?

Thank you! Vojta

Your scenario could be one of the rare cases where extending Thread
makes sense, as opposed to just passing it a runnable. You might want
to try something like this:

public class NotifyingThread extends Thread {
Set<ThreadListener> listeners = new
CopyOnWriteArraySet<ThreadListener>();

/* all constructors from superclass */

public Thread(){
super();
}

...

public void addThreadListener(ThreadListener tl){
listeners.add(tl);
}

public void removeThreadListener(ThreadListener tl){
listeners.remove(tl);
}

public void run(){
for (ThreadListener tl : listeners){
tl.threadStarting(this);
}

super.run();

for (ThreadListener tl : listeners){
tl.threadEnding(this);
}
}
}

public interface ThreadListener {
void threadStarting(NotifyingThread t);
void threadEnding(NotifyingThread t);
}
 
V

viraj

For the example you have given.. I think Thread.join() is the better
option.
But if you have multiple listeners on one thread, extending thread as
suggesting is more extensible and OO approach.

viraj
 
V

Vojta

Thank you all a lot for your answers! Please, let me one more question
concerning calling methods of ThreadListener interface: In Delphi if thread
is calling some method which operates with visual components (e.g. enable
Button) the call must be synchronized otherwise there could be conflict with
main application thread and application freezes. In Stefan's example should
be synchronisation implemented or it is not required?

Thank you all for you answers! Vojta
 
T

Thomas Hawtin

Your scenario could be one of the rare cases where extending Thread
makes sense, as opposed to just passing it a runnable. You might want
to try something like this:

Threads are a red herring here. You might have a thread pool or similar.
There is no need to override anything in Thread or add anything to the
class. If you want to separate to re-enabling of the of the button, then
that's quite simple with a Runnable:

new Runnable() { public void run() {
try {
doRun.run();
} finally {
EventQueue.invokeLater(new Runnable() {
public void run() {
button.setEnabled(true);
}
});
}
}}

Tom Hawtin
 
T

Thomas Hawtin

Vojta said:
Thank you all a lot for your answers! Please, let me one more question
concerning calling methods of ThreadListener interface: In Delphi if thread
is calling some method which operates with visual components (e.g. enable
Button) the call must be synchronized otherwise there could be conflict with
main application thread and application freezes. In Stefan's example should
be synchronisation implemented or it is not required?

Swing components should be accessed on the AWT Event Dispatch Thread.
You can run code on the EDT quite easily:

EventQueue.invokeLater(new Runnable() {
public void run() {
button.setEnabled(true);
}
});

You can check whether or not you are on the EDT with:

assert EventQueue.isDispatchThread();

(but remember to add -ea or -enableassertions on the command line)

Running everything from the same thread is roughly equivalent to running
it with a particular lock held. There are some difference in thread
priority and model dialogue boxes essentially release the lock in a
non-structured fashion.

Tom Hawtin
 
S

Stefan Schulz

Thomas said:
Threads are a red herring here. You might have a thread pool or similar.
There is no need to override anything in Thread or add anything to the
class. If you want to separate to re-enabling of the of the button, then
that's quite simple with a Runnable:

That solves the concrete problem, but not the more general interest in
being asynchronously notified when a given thread is about to
terminate.

That being said, i think both designs are valid. Your thread pool might
even pool specifically crafted threads. For example, the standard
thread pooling API explicitly offers a means of using a specific
implementation class via a ThreadFactory.
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top