2

I'm developing a multi-timertask project.

First of all, I design some classes extended TimerTask and override the run() method. In the run() method, a line will be printed with current time.

Secondly, a Timer is initialized like this.

......
DataTask task1 = new DataTask();
myTaskList.add(task1);
DataTask task2 = new DataTask();
myTaskList.add(task2);
DataTask task3 = new DataTask();
myTaskList.add(task3);
DataTask task4 = new DataTask();
myTaskList.add(task4);

for(TimerTask task : myTaskList)
{
    Timer timer = new Timer();
    timer.schedule(task,1,60*1000);
}

......

public class DataTask extends TimerTask
{
     @override
     public void run()
     {
           System.out.println("print sth");
     }

}

One task in one thread. Is that Right?

Sometimes the tasks work, however, sometimes the tasks will not print anything, without any Exception, while the thread is still alive.

What could be the reason for this?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
edward
  • 25
  • 1
  • 6
  • You you can get the the time by writing System.currentTimeMillis(). Many times the use of a timer class can be simply achieved with that. – Derek Sep 13 '11 at 06:00

1 Answers1

0

One task in one thread. Is that Right?

No, under the hood, the Timer utilizes a single thread for scheduling the submitted tasks but in your case since you are using multiple timers, yes, every task would execute in a separate thread. If you have the requirement to run multiple tasks by utilizing multiple threads, look into Executors in the concurrent package. Look into the Javadoc of ExecutorService class for examples.

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • Well, I know that Timer could schedule tasks and one task is in one thread. But I don't know what causes the problem. – edward Sep 13 '11 at 06:02
  • Need more details, what kind of problems are you talking about? Exceptions? Which exceptions in particular? I just tried out the code and it seems to be working fine. – Sanjay T. Sharma Sep 13 '11 at 06:07
  • -1 wrong...he is creating a new Timer for each task, and hence each task will be executed in its own thread. – Suraj Chandran Sep 13 '11 at 06:10
  • @Suraj: Well, I did mention that Timer utilizes a single thread under the hood. So multiple tasks submitted to the same Timer would use the same thread. That statement sounded a bit misleading hence my correction. – Sanjay T. Sharma Sep 13 '11 at 06:13
  • Thank you for your help. This code may work but it might have problem at some time while I invoke some SQL query and update in it. Maybe 1-2 days,maybe 2 weeks, I don't know. No Exception could be caught which confuse me. – edward Sep 13 '11 at 06:13
  • @edward: Then it's a bit more complicated. Have you tried wrapping the entire `run` method in the `DataTask` class in a try...catch block and printing out the caught `Throwable`? Also, is there any dependency between tasks? Since you are using multiple Timers, it is as good as tasks executing separately which might create problems if you have dependency among them. Also, have a look at the new way of creating and scheduling tasks in this thread: http://stackoverflow.com/questions/438312/how-to-schedule-a-callable-to-run-on-a-specific-time – Sanjay T. Sharma Sep 13 '11 at 06:19
  • @Sanjay T. Sharma: Thank you. I have try catch block in every task and I am sure that no dependency exists between tasks. I am reading the thread you provided. :) – edward Sep 13 '11 at 06:23