import java.util.concurrent.CountDownLatch;
public class CountdownLatchExample {
public static void main(String[] args) throws InterruptedException {
new Thread(new Worker(latch, "Worker1")).start();
new Thread(new Worker(latch, "Worker2")).start();
new Thread(new Worker(latch, "Worker3")).start();
System.out.println("All workers have completed their tasks");
}
}
class Worker implements Runnable {
private final CountDownLatch latch;
private final String name;
public Worker(CountDownLatch latch, String name) {
this.latch = latch;
this.name = name;
}
public void run() {
System.out.println(name + " is doing some work");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " has completed their task");
}
}
Worker2 is doing some work
Worker1 is doing some work
Worker3 is doing some work
Worker2 has completed their task
Worker1 has completed their task
Worker3 has completed their task
All workers have completed their tasks
import java.util.concurrent.Semaphore;
public class SemaphoreExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
new Thread(new Worker(semaphore, "Worker" + i)).start();
}
}
}
class Worker implements Runnable {
private final Semaphore semaphore;
private final String name;
public Worker(Semaphore semaphore, String name) {
this.semaphore = semaphore;
this.name = name;
}
public void run() {
try {
System.out.println(name + " is working");
System.out.println(name + " has finished their work");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
}
Worker1 is working
Worker4 is working
Worker3 is working
Worker4 has finished their work
Worker1 has finished their work
Worker3 has finished their work
Worker6 is working
Worker5 is working
Worker10 is working
Worker6 has finished their work
Worker2 is working
Worker5 has finished their work
Worker9 is working
Worker10 has finished their work
Worker7 is working
Worker9 has finished their work
Worker8 is working
Worker2 has finished their work
Worker7 has finished their work
Worker8 has finished their work
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample {
public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(3, () -> {
System.out.println("All threads have reached the barrier");
new Thread(new Worker(barrier, "Worker1")).start();
new Thread(new Worker(barrier, "Worker2")).start();
new Thread(new Worker(barrier, "Worker3")).start();
}
}
class Worker implements Runnable {
private final CyclicBarrier barrier;
private final String name;
public Worker(CyclicBarrier barrier, String name) {
this.barrier = barrier;
this.name = name;
}
public void run() {
System.out.println(name + " is doing some work");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " has reached the barrier");
try {
System.out.println(name + " continues execution");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Worker2 is doing some work
Worker1 is doing some work
Worker3 is doing some work
Worker2 has reached the barrier
Worker1 has reached the barrier
Worker3 has reached the barrier
All threads have reached the barrier
Worker3 continues execution
Worker1 continues execution
Worker2 continues execution