Skip to content

Commit 09b0851

Browse files
daemon threads vs actual worker threads, closes #77
1 parent 4d668af commit 09b0851

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
package com.learn.threading.daemon;public class App {
1+
package com.learn.threading.daemon;
2+
3+
public class App {
4+
5+
public static void main(String[] args) {
6+
7+
Thread daemon = new Thread(new DaemonRunnable());
8+
if (!daemon.isDaemon()) {
9+
daemon.setDaemon(true);
10+
}
11+
12+
Thread worker = new Thread(new WorkerRunnable());
13+
14+
daemon.start();
15+
worker.start();
16+
}
217
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
package com.learn.threading;
1+
package com.learn.threading.daemon;
22

3-
public class DeamonThread {
3+
public class DaemonRunnable implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
try {
8+
// infinite loop
9+
while (true) {
10+
System.out.println("Daemon thread is running...");
11+
Thread.sleep(1000);
12+
}
13+
} catch (InterruptedException e) {
14+
e.printStackTrace();
15+
}
16+
17+
18+
}
419
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
package com.learn.threading.daemon;public class WorkerThread {
1+
package com.learn.threading.daemon;
2+
3+
public class WorkerRunnable implements Runnable {
4+
@Override
5+
public void run() {
6+
System.out.println("Worker thread is running...");
7+
}
28
}

0 commit comments

Comments
 (0)