feat: Add comprehensive examples from multithreading and hashing lectures
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class InterruptExample {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread worker = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("👷 Worker started...");
|
||||
|
||||
try {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.println(" worker: stage " + i);
|
||||
Thread.sleep(1000); // Each step takes 1 second
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("🛑 Worker: Hey! Someone cut me off!");
|
||||
System.out.println("👷 Worker: OK, I'm finishing...");
|
||||
}
|
||||
|
||||
System.out.println("👷 Goodbye worker!");
|
||||
}
|
||||
});
|
||||
|
||||
worker.start();
|
||||
|
||||
Thread.sleep(6500); // Let the worker work for 6.5 seconds
|
||||
|
||||
System.out.println("💀 Main thread: That's enough! Cut it!");
|
||||
worker.interrupt(); // Tells the worker to stop
|
||||
|
||||
worker.join();
|
||||
System.out.println("Everything is finished!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class IsAliveExample {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread worker = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("👷 Worker started...");
|
||||
try {
|
||||
Thread.sleep(15000); // Runs for 15 seconds
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("👷 The worker is done!");
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("before start: " + worker.isAlive()); // false
|
||||
|
||||
worker.start();
|
||||
|
||||
System.out.println("Immediately after start: " + worker.isAlive()); // true
|
||||
|
||||
Thread.sleep(1000); // We wait 1 second
|
||||
System.out.println("After 1 second: " + worker.isAlive()); // true (still works)
|
||||
|
||||
worker.join(); // We wait for the worker to finish
|
||||
|
||||
System.out.println("After completion of work: " + worker.isAlive()); // false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class IsInterruptedExample {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
Thread counter = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int number = 0;
|
||||
|
||||
// Continue until interrupted
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
number++;
|
||||
System.out.println("Count: " + number);
|
||||
|
||||
try {
|
||||
Thread.sleep(50); // 50 milliseconds between each number
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted while sleeping!");
|
||||
break;
|
||||
}
|
||||
|
||||
if (number >= 100) {
|
||||
break; // We reached 100, don't continue
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Thread stopped! Last count: " + number);
|
||||
}
|
||||
});
|
||||
|
||||
counter.start();
|
||||
|
||||
// Let it work for 2 seconds, then stop it
|
||||
Thread.sleep(2000);
|
||||
System.out.println("I'm disconnecting...");
|
||||
counter.interrupt();
|
||||
|
||||
counter.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
class WorkerTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("👷 Worker started...");
|
||||
try {
|
||||
Thread.sleep(10000); // Runs for 10 seconds
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("👷 The worker finished his work!");
|
||||
}
|
||||
}
|
||||
|
||||
public class JoinSimpleExample {
|
||||
public static void main(String[] args) {
|
||||
WorkerTask task = new WorkerTask();
|
||||
Thread worker = new Thread(task);
|
||||
|
||||
worker.setName("Worker-Thread");
|
||||
worker.start();
|
||||
|
||||
System.out.println("The main thread waits until the worker finishes...");
|
||||
|
||||
try {
|
||||
worker.join(); // The main thread stops here until the worker finishes
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("Main thread continues - now that the worker is finished!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class MyRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Runnable is running: " + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class MyThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Thread is running: " + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class SleepExample {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("starting program");
|
||||
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.println("Count: " + i);
|
||||
|
||||
try {
|
||||
Thread.sleep(1000); // 1 second stop
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Thread terminated!");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("End of program");
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class ThreadNamingExample {
|
||||
public static void main(String[] args) {
|
||||
// Create a thread without a specified name
|
||||
Thread thread1 = new MyThread();
|
||||
|
||||
// Set thread name
|
||||
thread1.setName("Worker-1");
|
||||
|
||||
// Get and print the name of the thread
|
||||
System.out.println("Thread name before start: " + thread1.getName());
|
||||
thread1.start();
|
||||
|
||||
// Main thread name
|
||||
Thread mainThread = Thread.currentThread();
|
||||
System.out.println("main thread name: " + mainThread.getName());
|
||||
|
||||
// Rename the main thread
|
||||
mainThread.setName("Main-Thread");
|
||||
System.out.println("New name of main thread: " + mainThread.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class ThreadVsRunnable {
|
||||
public static void main(String[] args) {
|
||||
|
||||
MyThread thread1 = new MyThread();
|
||||
thread1.start();
|
||||
|
||||
Thread thread2 = new Thread(new MyRunnable());
|
||||
thread2.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user