PureNSimple
Programmer
Hi
I am trying to schedule a recurring task using Timer and TimerTask classes. What I could not understand is how to schedule my task for an indefinite time. I am creating a Timer object. Then I ask it to schedule a TimerTask by passing it an instance of my TimerTask subclass. What I wish is to execute the TimerTask every two hours forever. However, I am having trouble setting this up. Here is the code
Timer timer = new Timer(true);
//create a mailtask
MailTask timerTask = new MailTask(null);
String[] list = new String[] {"pureNSimple@tek-tips.com"};
timerTask.setRecipients(Arrays.asList(list));
//schedule task
timer.schedule(timerTask, 0, 1000*60*60*2);
try {
Thread.currentThread().sleep(1000*60*60*4); problem...the main thread will terminate after 4 hours and with it the scheduled task will cease working
}
catch (Exception ex) {
}
Any idea of how to schedule a task to work forever will be highly appreciated.
Regards,
Pure
I am trying to schedule a recurring task using Timer and TimerTask classes. What I could not understand is how to schedule my task for an indefinite time. I am creating a Timer object. Then I ask it to schedule a TimerTask by passing it an instance of my TimerTask subclass. What I wish is to execute the TimerTask every two hours forever. However, I am having trouble setting this up. Here is the code
Timer timer = new Timer(true);
//create a mailtask
MailTask timerTask = new MailTask(null);
String[] list = new String[] {"pureNSimple@tek-tips.com"};
timerTask.setRecipients(Arrays.asList(list));
//schedule task
timer.schedule(timerTask, 0, 1000*60*60*2);
try {
Thread.currentThread().sleep(1000*60*60*4); problem...the main thread will terminate after 4 hours and with it the scheduled task will cease working
}
catch (Exception ex) {
}
Any idea of how to schedule a task to work forever will be highly appreciated.
Regards,
Pure