Problem with timer-task in java.

I have a vaadin application, where I am scheduling tasks by giving each task a specific time.

I am using TimerTask from java to schedule the tasks. I can schedule the task according to time.

But How can I cancel the tasks from TimerTask in java? We’ve a method timer.cancel(); but it olny cancels the tasks which are ongoing in the executions.

How can I get the list of tasks queued?
OR
How can I cancel all the tasks in TimerTask queue?

Anyone?

Thanks!

We’ve a method timer.cancel(); but it olny cancels the tasks which are ongoing in the executions.

I think you have misunderstood it, i.e. it is just the opposite, here is what TimerTask#cancel() JavaDoc says:

"Cancels this timer task. If the task has been scheduled for one-time execution and has not yet run, or has not yet been scheduled, it will never run. If the task has been scheduled for repeated execution, it will never run again. (If the task is running when this call occurs, the task will run to completion, but will never run again.) "

Tatu Lund:

We’ve a method timer.cancel(); but it olny cancels the tasks which are ongoing in the executions.

I think you have misunderstood it, i.e. it is just the opposite, here is what TimerTask#cancel() JavaDoc says:

"Cancels this timer task. If the task has been scheduled for one-time execution and has not yet run, or has not yet been scheduled, it will never run. If the task has been scheduled for repeated execution, it will never run again. (If the task is running when this call occurs, the task will run to completion, but will never run again.) "

Oh, Okay!

I have one more question.

How much trustworthy is TimerTask? I mean How many tasks can be scheduled in the queue if we never cancel any of them?

You should probably use ScheduledThreadPoolExecutor instead of Timer nowadays: https://howtodoinjava.com/java/multi-threading/task-scheduling-with-executors-scheduledthreadpoolexecutor-example/