aspose file tools*
The moose likes Java in General and the fly likes implementing a timed/scheduled task Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "implementing a timed/scheduled task" Watch "implementing a timed/scheduled task" New topic
Author

implementing a timed/scheduled task

Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
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

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hey Junilu,
We seem to be threads in a race condition tonight.
Michael Morris
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

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

Joined: Feb 26, 2001
Posts: 4456
    
    6

(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

Joined: Jan 30, 2002
Posts: 3451
And if I may, the At command on NT kernel machines (NT, 2000, XP).
Michael Morris
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
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

Joined: Jan 30, 2002
Posts: 3451
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: implementing a timed/scheduled task