feat: Add comprehensive examples from multithreading and hashing lectures
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user