moved daemon thread examples from core-java-concurrency-advanced to core-java-concurrency-advanced-2

This commit is contained in:
fejera
2019-09-29 11:14:11 +02:00
parent 432395628b
commit c98254dbe0
4 changed files with 24 additions and 1 deletions

View File

@@ -4,3 +4,4 @@
### Relevant Articles:
- [Semaphores in Java](https://www.baeldung.com/java-semaphore)
- [Daemon Threads in Java](https://www.baeldung.com/java-daemon-thread)

View File

@@ -0,0 +1,23 @@
package com.baeldung.concurrent.daemon;
public class NewThread extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
while (true) {
for (int i = 0; i < 10; i++) {
System.out.println(this.getName() + ": New Thread is running..." + i);
try {
//Wait for one sec so it doesn't print too fast
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// prevent the Thread to run forever. It will finish it's execution after 2 seconds
if (System.currentTimeMillis() - startTime > 2000) {
Thread.currentThread().interrupt();
break;
}
}
}
}

View File

@@ -14,7 +14,6 @@
- [CyclicBarrier in Java](https://www.baeldung.com/java-cyclic-barrier)
- [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
- [Daemon Threads in Java](https://www.baeldung.com/java-daemon-thread)
- [Priority-based Job Scheduling in Java](https://www.baeldung.com/java-priority-job-schedule)
- [Brief Introduction to Java Thread.yield()](https://www.baeldung.com/java-thread-yield)
- [Print Even and Odd Numbers Using 2 Threads](https://www.baeldung.com/java-even-odd-numbers-with-2-threads)