In C#, the timer will trigger an event at a specific interval when enabled. How do I achieve this in Java?
I want to make a method to be run at a specific interval. I know how to do this in C#, but not Java.
Code in C#:
private void timer1_Tick(object sender, EventArgs e)
{
//the method
}
I tried Timer
and TimerTask
, but I am not sure whether the method will run when other methods are running.
You can use the codeplex library to implement this.
Schedule a task to run every second with initial delay of 5 seconds
Schedule a task to run everyday at 3 AM
You can use javax.swing.Timer. It has
delay
in constructor:You are looking at the right classes. The Timer and TimerTask are the right ones, and they will run in the background if you use them something like this:
One way is to use the ExecutorService: