Skip to content

Instantly share code, notes, and snippets.

@mr5z
Created November 10, 2015 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr5z/b3078ba5e497c618a0a7 to your computer and use it in GitHub Desktop.
Save mr5z/b3078ba5e497c618a0a7 to your computer and use it in GitHub Desktop.
A service thread that polls the command queue
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new Executor());
t.start();
System.out.println("I'm from the main thread!");
Thread.sleep(5000);
try {
System.out.println("Interrupting another thread...");
t.interrupt();
System.out.println("Success!");
}
catch(Exception e) {
System.out.println("Failed: " + e.getMessage());
}
}
static class Executor implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch(Exception e) {
break;
}
System.out.println("I'm from another thread!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment