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 / February 2006

Tip: Looking for answers? Try searching our database.

Thread - on terminate event

Thread view: 
Vojta - 19 Feb 2006 20:49 GMT
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
im - 20 Feb 2006 01:36 GMT
> Hello,
>
[quoted text clipped - 4 lines]
> 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
Stefan Schulz - 20 Feb 2006 01:56 GMT
> Hello,
>
[quoted text clipped - 6 lines]
>
> 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);
}
Vojta - 20 Feb 2006 09:56 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?

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/

Thomas Hawtin - 20 Feb 2006 10:31 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
>> 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.
viraj - 20 Feb 2006 06:42 GMT
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


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



©2008 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.