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
>> Hello,
>>
[quoted text clipped - 49 lines]
> void threadEnding(NotifyingThread t);
> }
Thomas Hawtin - 20 Feb 2006 11:06 GMT
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
>> 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?
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Stefan Schulz - 20 Feb 2006 16:48 GMT
> >> 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
[quoted text clipped - 11 lines]
> 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.