Time interval in Java -
how call method after time interval? e.g if want print statement on screen after 2 second, procedure?
system.out.println("printing statement after every 2 seconds");
the answer using javax.swing.timer , java.util.timer together:
private static javax.swing.timer t; public static void main(string[] args) { t = null; t = new timer(2000,new actionlistener() { @override public void actionperformed(actionevent e) { system.out.println("printing statement after every 2 seconds"); //t.stop(); // if want 1 print uncomment line } }); java.util.timer tt = new java.util.timer(false); tt.schedule(new timertask() { @override public void run() { t.start(); } }, 0); } obviously can achieve printing intervals of 2 seconds use of java.util.timer only, if want stop after 1 printing difficult somehow.
also not mix threads in code while can without threads!
hope helpful!
Comments
Post a Comment