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 / November 2005

Tip: Looking for answers? Try searching our database.

Custom Java sleeper thread not triggering reliably

Thread view: 
joshuamcgee@gmail.com - 17 Nov 2005 22:38 GMT
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
Knute Johnson - 18 Nov 2005 00:41 GMT
> 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()
[quoted text clipped - 3 lines]
>
> Joshua McGee

Look at java.util.Timer.scheduleAtFixedRate()

Signature

Knute Johnson
email s/nospam/knute/

jigounov@gmail.com - 18 Nov 2005 02:58 GMT
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.
Roedy Green - 18 Nov 2005 14:24 GMT
>Any help would be appreciated.

If you just want to get it going, use a timer instead. See
http://mindprod.com/jgloss/timer.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

joshuamcgee@gmail.com - 21 Nov 2005 04:17 GMT
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


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



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