Created
December 8, 2018 12:11
-
-
Save ani03sha/9ed8fc1d4f248ff0c651eb63af803e22 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 org.redquark.kickstarter.threads; | |
public class DaemonImplementation { | |
public static void main(String[] args) { | |
DaemonThread daemonThread = new DaemonThread(); | |
daemonThread.start(); | |
try{ | |
Thread.sleep(3000); | |
} catch (InterruptedException e){ | |
e.printStackTrace(); | |
} | |
System.out.println("Main thread finishes"); | |
} | |
static class DaemonThread extends Thread { | |
DaemonThread(){ | |
// When false, (i.e. when it's a user thread), the Worker thread continues to run. | |
// When true, (i.e. when it's a daemon thread), the Worker thread terminates when the main thread terminates. | |
setDaemon(true); | |
} | |
@Override | |
public void run() { | |
int count = 0; | |
while (true){ | |
System.out.println("Count from Daemon: " + count++); | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e){ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment