Custom Java sleeper thread not triggering reliably

Discussion in 'Java' started by joshuamcgee@gmail.com, Nov 17, 2005.

  1. Guest

    Hello all,

    I needed a way for my class to sleep for a specified amount of time,
    then wake up and perform an operation, in a non-blocking fashion. I
    came up with the following:

    //************************************************************
    private class SleeperThread extends Thread {
    long _id;
    long _sleepTo;
    Executive _parent;
    SleeperThread(long sleepTo, long id, Executive parent) {
    _id = id;
    _sleepTo = sleepTo;
    _parent = parent;
    }

    public synchronized void run() {
    long interval = _sleepTo - time();
    if (interval > 0) {
    try {
    wait(interval);
    }
    catch (InterruptedException ex) {
    System.out.println("Sleep interrupted");
    return;
    }
    }
    _parent.timeTrigger(_id, _sleepTo);
    }
    }
    //************************************************************

    This class is constructed with a long representing the time in epoch
    millis to sleep until, a long timer ID, and a pointer to its parent.
    The run method waits for the desired duration, then calls a method in
    the parent called "timeTrigger". It would be invoked like this:

    //************************************************************
    SleeperThread sleeperThread = new SleeperThread(time, sleeperID,
    this);
    sleeperThread.start();
    //************************************************************

    This seemed like an elegant solution, and it works sometimes, but other
    times timeTrigger is never called, in an unpredictable fashion. It is
    hard to debug the multi-thread behavior in JBuilder.

    Am I making an invalid assumption? Have I made a coding blunder? Is
    there a built-in way to do this? Note that I cannot just call wait()
    in the main thread, because that would block the thread's execution.

    Any help would be appreciated.

    Joshua McGee
     
    , Nov 17, 2005
    #1
    1. Advertising

  2. wrote:
    > Am I making an invalid assumption? Have I made a coding blunder? Is
    > there a built-in way to do this? Note that I cannot just call wait()
    > in the main thread, because that would block the thread's execution.
    >
    > Any help would be appreciated.
    >
    > Joshua McGee
    >


    Look at java.util.Timer.scheduleAtFixedRate()

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 18, 2005
    #2
    1. Advertising

  3. Guest

    1. replace wait() with Thread.sleep()

    2. Depending on what call _parent.timeTrigger(_id, _sleepTo); does you
    might want to call it outside of synchronized block:


    public void run() {
    ....
    synchronized(this) {
    try {
    Thread.sleep()
    } catch(...) {
    ...
    }
    }
    _parent.timeTrigger(_id, _sleepTo);
    }

    Good luck.
     
    , Nov 18, 2005
    #3
  4. Roedy Green Guest

    On 17 Nov 2005 14:38:33 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >Any help would be appreciated.


    If you just want to get it going, use a timer instead. See
    http://mindprod.com/jgloss/timer.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 18, 2005
    #4
  5. Guest

    Thank you, that's very much like what I was trying to duplicate. I was
    initially worried about tasks bunching due to slowish execution, but I
    think all the tasks will be rather quick (just sending a UDP packet to
    another component.)

    Thank you to the other two who advised. I'll post again if anything
    interesting develops.

    Best,

    Joshua McGee
     
    , Nov 21, 2005
    #5
    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. =?Utf-8?B?QW5kcmV3?=

    How to reliably redirect from login page?

    =?Utf-8?B?QW5kcmV3?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    388
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 10, 2006
  2. Matthew Barnes

    Detecting interactive mode reliably

    Matthew Barnes, Sep 8, 2003, in forum: Python
    Replies:
    0
    Views:
    275
    Matthew Barnes
    Sep 8, 2003
  3. Gernot Hillier
    Replies:
    1
    Views:
    546
    Josiah Carlson
    Feb 21, 2004
  4. Wayne Brantley

    Properties do not reliably persist using CompositeControl

    Wayne Brantley, Dec 7, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    117
    Walter Wang [MSFT]
    Dec 12, 2006
  5. RT
    Replies:
    0
    Views:
    163
Loading...

Share This Page