35 lines
996 B
Java
35 lines
996 B
Java
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!");
|
|
}
|
|
} |