How to relaunch a TimerTask

后端 未结 1 1733
迷失自我
迷失自我 2021-01-24 16:12

I have written a task to send a certain TCP message through a socket. I have a file with a bunch of messages and some timestamps, so I programmed the task as a TimerTask, and I

相关标签:
1条回答
  • 2021-01-24 16:17

    A TimerTask is not designed to be rescheduled and it is the Timer that manages the (single) thread.

    Use one Timer and many new TimerTasks:

    Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially ..

    After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread [should] terminates gracefully (and becomes subject to garbage collection).

    [From each of the schedule methods:]

    Throws IllegalStateException if [the TimerTask] was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

    If there are indeed many threads spawned by a single Timer, then that would be a bug which is unlikely: make sure there really is only one Timer object being used.

    The last question, of how to compose individual events into a workflow, should be a separate post.

    0 讨论(0)
提交回复
热议问题