Created
July 22, 2018 13:49
-
-
Save RobinCheptileh/7136a4077bb182fcd85e8d990ca4da19 to your computer and use it in GitHub Desktop.
Revisions
-
RobinCheptileh created this gist
Jul 22, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ import java.util.Timer; import java.util.TimerTask; public class Main { public static void main(String[] args) { // Instantiate new timer Timer timer = new Timer(true); System.out.println("Timer started!"); // Schedule a timer for every second // Declare anonymous TimerTask timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("Timer!"); } }, 0, 1000); // Sleep thread for 20 seconds try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } // Cancel timer after 20 seconds timer.cancel(); System.out.println("Timer stopped!"); } }