I am new to JAVA. After reviewing a few examples - I wrote the semblance of a stopwatch using Timer. After executing the program, 500 minutes of the stopwatch pass in 2 to 3 seconds. Apparently somewhere something messed up with syntax. I would be grateful for the help. Also I will accept any suggestions for optimizing this code.

import java.util.Timer; import java.util.TimerTask; public class ut extends TimerTask { Timer timer; public void run(){ int seconds = 0, minutes = 0; while (true) { seconds++; if (minutes != 0) System.out.print(minutes + ":"); System.out.println(seconds); if (seconds == 59) { seconds = -1; minutes++; } if (minutes == 500){ timer.cancel(); } } } public static void main(String[] args) { new Timer().schedule(new ut(), 10); } } 

I also read that you can somehow implement this task through the swing. If anyone knows how to implement it and whether it will be easier than this example, I will be grateful for the help.

  • The issue is resolved. Thank you all for your help. - WeB_Master 1:16 pm
  • what, you were told that the time should be in milliseconds, it is (1sec x 1000) - Gorets

2 answers 2

 import javax.swing.Timer; ... // Будет вызываться каждую секунду timer = new Timer(1000, new ActionListener( public void actionPerformed(ActionEvent ev) { System.out.println("WOW!"); } )); timer.start(); 

Source of

    In Java, the times are in milliseconds.