Skip to content

Instantly share code, notes, and snippets.

@fty4
Created May 17, 2017 09:05
Show Gist options
  • Save fty4/0fc3f77ac19df95d20420be90972c2a5 to your computer and use it in GitHub Desktop.
Save fty4/0fc3f77ac19df95d20420be90972c2a5 to your computer and use it in GitHub Desktop.
Create some Thread / Timer
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