Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save americanstone/93c8e395357858df82c3ccc5742bf41a to your computer and use it in GitHub Desktop.
Save americanstone/93c8e395357858df82c3ccc5742bf41a to your computer and use it in GitHub Desktop.
When running this code with the line commented, the thread is not deamon, so even if your main method has finished, you'll keep on having the console flooded until you stop it with CTRL+C. That is, the JVM will not exit.
public class Spawner {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
System.out.println("I'm still alive");
}
}
});
t.start();
// Try uncommenting/commenting this line
// t.setDaemon(true);
System.out.println("Main thread has finished");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment