moved daemon thread examples from core-java-concurrency-advanced to core-java-concurrency-advanced-2
This commit is contained in:
@@ -4,3 +4,4 @@
|
|||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Semaphores in Java](https://www.baeldung.com/java-semaphore)
|
- [Semaphores in Java](https://www.baeldung.com/java-semaphore)
|
||||||
|
- [Daemon Threads in Java](https://www.baeldung.com/java-daemon-thread)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,6 @@
|
|||||||
- [CyclicBarrier in Java](https://www.baeldung.com/java-cyclic-barrier)
|
- [CyclicBarrier in Java](https://www.baeldung.com/java-cyclic-barrier)
|
||||||
- [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
|
- [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)
|
- [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)
|
- [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)
|
- [Print Even and Odd Numbers Using 2 Threads](https://www.baeldung.com/java-even-odd-numbers-with-2-threads)
|
||||||
|
|||||||
Reference in New Issue
Block a user