Thread - on terminate event

Discussion in 'Java' started by Vojta, Feb 19, 2006.

  1. Vojta

    Vojta Guest

    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
     
    Vojta, Feb 19, 2006
    #1
    1. Advertising

  2. Vojta

    im Guest

    On Sun, 19 Feb 2006 21:49:14 +0100, Vojta wrote:

    > 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
     
    im, Feb 20, 2006
    #2
    1. Advertising

  3. Vojta wrote:
    > 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);
    }
     
    Stefan Schulz, Feb 20, 2006
    #3
  4. Vojta

    viraj Guest

    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
     
    viraj, Feb 20, 2006
    #4
  5. Vojta

    Vojta Guest

    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

    "Stefan Schulz" <> píse v diskusním príspevku
    news:...
    >
    > Vojta wrote:
    >> 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);
    > }
    >
     
    Vojta, Feb 20, 2006
    #5
  6. Stefan Schulz wrote:
    > Vojta wrote:
    >>
    >> 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Feb 20, 2006
    #6
  7. Vojta wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Feb 20, 2006
    #7
  8. Thomas Hawtin wrote:

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


    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.
     
    Stefan Schulz, Feb 20, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    534
    Peter
    Aug 28, 2004
  2. Edmond wong
    Replies:
    4
    Views:
    536
    iksrazal
    Mar 29, 2005
  3. Adrian Casey

    Using signal.alarm to terminate a thread

    Adrian Casey, Nov 13, 2006, in forum: Python
    Replies:
    7
    Views:
    631
    Nick Craig-Wood
    Nov 15, 2006
  4. Replies:
    4
    Views:
    1,231
    Flash Gordon
    Aug 24, 2005
  5. Replies:
    9
    Views:
    763
Loading...

Share This Page