Last active
September 12, 2019 07:50
-
-
Save martijndwars/a5a5065c80be34ba299548313f195d97 to your computer and use it in GitHub Desktop.
ThreadDeath example. The main thread starts a new thread, then stops the new thread. The new thread catches the ThreadDeath exception, giving it a chance to clean up, before rethrowing the exception and terminating.
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
package nl.martijndwars.oshell; | |
public class Foo { | |
public static void main(String[] args) throws InterruptedException { | |
Thread thread = new Thread(new Bar()); | |
thread.start(); | |
Thread.sleep(3000); | |
thread.stop(); | |
} | |
static class Bar implements Runnable { | |
@Override | |
public void run() { | |
try { | |
repeat(); | |
} catch (ThreadDeath e) { | |
System.err.println("Caught ThreadDeath, but I will loop anyway. Muahahah!"); | |
repeat(); | |
} | |
} | |
private void repeat() { | |
while (true) { | |
System.out.println("Hi"); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment