Created
May 17, 2017 09:05
-
-
Save fty4/0fc3f77ac19df95d20420be90972c2a5 to your computer and use it in GitHub Desktop.
Create some Thread / Timer
This file contains 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 characters
public class ThreadExample { | |
public static void main(String[] args) { | |
try { | |
System.out.println("Starting Thread..."); | |
runner r = new runner(); | |
r.start(); | |
Thread.sleep(5_000); | |
r.halt(); | |
System.out.println("Stopped Thread..."); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static class runner extends Thread { | |
private boolean running = true; | |
public void run() { | |
while (running) { | |
try { | |
// Do something... | |
System.out.println("Thread: still there"); | |
Thread.sleep(1_000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public void halt() { | |
running = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment