Link to home
Start Free TrialLog in
Avatar of chencc77
chencc77

asked on

Timer and Threads

I create a database to store date for jobs to be executed. I use Timer.schedule(TimerTask task, Date time) to do the scheduling. TimerTask will invoke a thread to run certain code. The program will the check the current date that match with date in database. If exist, it will run the TimerTask.
If i have 1000 of jobs would be run at 10.00 AM and 1000 of jobs would be run at 10.01AM. During the checking process and current time is 10.00 AM. If the checking process for 1000 jobs at 10.00 AM take more than 1 minute. how do i make sure that jobs at 10.01 AM will be executed.
I hope my question is clear to you. Thanks.
Avatar of Venci75
Venci75

In my appinion - you have two ways to do this:
1. When get the jobs from the database - select all jobs not only for (for example) 10:05, but all jobs that must be started since the last operation. If your last operation was at 10:00, at 10:05 you will execute all jobs for 10:01, 10:02, 10:03, 10:04 and 10:05.

2. Make sure that the jobs are started by separate thread - not the thread that checks the database. This way you will minimize the time of this thread.
1. TimerTask should handle this 'problem' itself ?

2. from the docs.

> . Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
Avatar of chencc77

ASKER

Thanks for ur help. but i not sure which method is working. i hope there are more answers.
IMHO TimerTask will always execute all the jobs.

did you tried some simpletest ?

did i need to create new Timer for each Job? if i have 1000 of jobs, so i will have 1000 of Timer, izit correct way to create schedules?
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial