Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implementing a timed/scheduled task

 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building a web-app that has its own task scheduling. So far, I have made a task class where you can set the initial start time and the interval between executes. The task object sleeps until its next scheduled run time, then executes, then starts the cycle over again.
So my problem is this: These tasks can be modified at any time - so, if the task is modified, and the task is currently sleeping (Thread.sleep()), how can I wake the thread up, modify the values, and start it again? I thought initially I could do some blocking of some sort, but that wouldn't work since I also need it to operate on a timed schedule. I'm thinking I need to throw an exception of some kind, but I can't figure out how to throw an exception from outside the class.... any ideas?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,
You should probably consider using java.util.Timer and java.util.TimerTask instead of Thread.sleep().
Look at this code:

As you can see with Timer.cancel() you can stop a task at will unless that task has already entered the TimerTask's run() method.
Michael Morris
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a reference to a sleeping thread, t, you can interrupt it by calling it's interrupt() method, e.g.
t.interrupt(); // go figure
However, I'm not sure the design you have is ideal. I could be wrong but the design that comes to my mind is for tasks to implement Runnable. Tasks would be defined and queued up, probably in some kind of list. Then you'll have a single monitoring thread, a TaskScheduler, that looks at the task at the head of the queue and then goes to sleep until it's time to kick off that task.
The point is, you'll only have one sleeping Thread: the TaskScheduler thread. If the task at the head of the queue is modified, then you would put it back in the queue where it needs to be based on it's schedule/interval, wake up the TaskScheduler by calling its interrupt() method and make it look at the current head-of-the-queue task (which may be the edited one or, if the tasks were reordered, a new one). The TaskScheduler would, if necessary, again go to sleep until the new task needs to be kicked off.
I'm sure there are other design considerations but I think that would be the main direction.
The advantage to this design would be that task objects don't need to be created until they are really needed and they go away once they are done. With your current design (as I understand it), you could potentially have hundreds or thousands of sleeping threads just lying around waiting to be woken up to get to work.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Junilu,
We seem to be threads in a race condition tonight.
Michael Morris
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know. I'm waiting for my carpet to dry--I just shampooed it. I'll be out of your way soon

class Task implements Runnable { ... }
Runnable removePukeSmell = new Task();
Thread shampooCarpet = new Thread(removePukeSmell);
shampooCarpet.start();
while (! done) {
shampooCarpet.wait();
...
}
if (done) {
Thread.sleep(8 * 3600);
}
[ March 28, 2003: Message edited by: Junilu Lacar ]
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(waking up after a very short sleep due to programming error above )
Phil,
You might also want to read up on how the UNIX cron utility works. It does essentially what you want to do, I guess.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if I may, the At command on NT kernel machines (NT, 2000, XP).
Michael Morris
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to do this w/o the help of an OS scheduler, in order for the program to be
1) self-contained
2) not require ISP assistance (it's for a webapp)
3) easily modifiable
etc, etc.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then take a look at Junilu's and my posts. Disregarding the thread metaphor humor of course
Michael Morris
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One cool thing about Timer is that it manages any number of timed tasks on one thread instead of starting a thread for each. Before I got clued into this, I wrote my own scheduler that keeps a collection of scheduled events. It knows what the "next" one is and sleeps until then. Any time you execute, add, change or remove events, it has to go through the collection again to determine the "next". I'm guessing Timer does much the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic