import java.io.IOException;
import java.io.PrintStream;


public class DaemonExample {
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 5; i++) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("Thread 1, Tick " + i);
				}
			}
		});
		
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 5; i++) {
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					System.out.println("Thread 2, Tick " + i);
				}
			}
		});
		
		System.out.println("Daemon by default? " + thread1.isDaemon());
		
		// Thread 1 is a user thread, program execution won't end until it does.
		thread1.setDaemon(false);
		// Thread 2 is a daemon thread, the JVM can shut down before it finishes.
		thread2.setDaemon(true);
		
		thread1.start();
		thread2.start();
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("Main thread finished all work");
	}
}

