How to make a timer?
I want to do Timer
that waits for 400 MSc and then goes and prints “hello!”. (eg.). I know how to do it throughjavax.swing.Timer
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("hi!");
}
};
a plus:
timer = new Timer(0, action);
timer.setRepeats(false);
timer.setInitialDelay(400);
timer.start();
but as I know it is definitely not very good since this type Timer
works for Swing. how to do it right? (without use Thread.sleep()
)
source to share
You may want to consider the Quartz Scheduler, which is a truly scalable, easily trainable and customizable solution. You can check the tutorials on the official website.
http://quartz-scheduler.org/documentation/quartz-2.1.x/quick-start
source to share
TimeUnit.MILLISECONDS.sleep(150L);
is an alternative;
You can also take a look at this Question
Which suggests using a loop while
that just waits or a ScheduledThreadPoolExecutor
source to share