Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Custom Java sleeper thread not triggering reliably

Reply
Thread Tools

Custom Java sleeper thread not triggering reliably

 
 
joshuamcgee@gmail.com
Guest
Posts: n/a
 
      11-17-2005
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

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-18-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
jigounov@gmail.com
Guest
Posts: n/a
 
      11-18-2005
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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-18-2005
On 17 Nov 2005 14:38:33 -0800, (E-Mail Removed) 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.
 
Reply With Quote
 
joshuamcgee@gmail.com
Guest
Posts: n/a
 
      11-21-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Properties do not reliably persist using CompositeControl Wayne Brantley ASP .Net Web Controls 2 12-12-2006 07:04 AM
Light Sleeper : Widescreen DVD ?? JAQK DVD Video 1 02-19-2006 08:31 PM
CookieContainer not working reliably ASP.NET webclient RT ASP .Net Web Services 0 08-04-2005 02:33 PM
Panasonic DMZ-FZ20, sleeper? Bill Crocker Digital Photography 93 04-06-2005 08:20 PM
Triggering a Ctrl-Break from within Java Schippy Java 3 12-12-2003 08:51 PM



Advertisments