Files
WS-07-Multithreading-Basics…/src/main/java/lecture/multithreading/basics/StandardPattern.java
T

33 lines
1.1 KiB
Java

package lecture.multithreading.basics;
public class StandardPattern {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(new Runnable() {
@Override
public void run() {
// Both together: checking in the loop
while (!Thread.currentThread().isInterrupted()) {
// doing work
System.out.println("Working...");
// Work simulation
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// When we are interrupted in sleep
System.out.println("Interrupted during sleep!");
Thread.currentThread().interrupt(); // Reset status
// break;
}
}
System.out.println("Thread stopped!");
}
});
worker.start();
Thread.sleep(1000);
worker.interrupt(); // Send a sign
worker.join();
}
}