2 threads start at the same time (#10972)
* 2 threads start at the same time * remove blank leading/trailing lines
This commit is contained in:
parent
d1d9ada490
commit
8011badd12
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.threadsstartatsametime;
|
||||
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
|
||||
public class ThreadsStartAtSameTime {
|
||||
|
||||
public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
|
||||
usingCountDownLatch();
|
||||
|
||||
Thread.sleep(30);
|
||||
|
||||
usingCyclicBarrier();
|
||||
}
|
||||
|
||||
private static void usingCountDownLatch() throws InterruptedException {
|
||||
System.out.println("===============================================");
|
||||
System.out.println(" >>> Using CountDownLatch <<<<");
|
||||
System.out.println("===============================================");
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
WorkerWithCountDownLatch worker1 = new WorkerWithCountDownLatch("Worker with latch 1", latch);
|
||||
WorkerWithCountDownLatch worker2 = new WorkerWithCountDownLatch("Worker with latch 2", latch);
|
||||
|
||||
worker1.start();
|
||||
worker2.start();
|
||||
|
||||
Thread.sleep(10);//simulation of some actual work
|
||||
|
||||
System.out.println("-----------------------------------------------");
|
||||
System.out.println(" Now release the latch:");
|
||||
System.out.println("-----------------------------------------------");
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
private static void usingCyclicBarrier() throws BrokenBarrierException, InterruptedException {
|
||||
System.out.println("\n===============================================");
|
||||
System.out.println(" >>> Using CyclicBarrier <<<<");
|
||||
System.out.println("===============================================");
|
||||
|
||||
CyclicBarrier barrier = new CyclicBarrier(3);
|
||||
|
||||
WorkerWithCyclicBarrier worker1 = new WorkerWithCyclicBarrier("Worker with barrier 1", barrier);
|
||||
WorkerWithCyclicBarrier worker2 = new WorkerWithCyclicBarrier("Worker with barrier 2", barrier);
|
||||
|
||||
worker1.start();
|
||||
worker2.start();
|
||||
|
||||
Thread.sleep(10);//simulation of some actual work
|
||||
|
||||
System.out.println("-----------------------------------------------");
|
||||
System.out.println(" Now open the barrier:");
|
||||
System.out.println("-----------------------------------------------");
|
||||
barrier.await();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.threadsstartatsametime;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class WorkerWithCountDownLatch extends Thread {
|
||||
private CountDownLatch latch;
|
||||
|
||||
public WorkerWithCountDownLatch(String name, CountDownLatch latch) {
|
||||
this.latch = latch;
|
||||
setName(name);
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
try {
|
||||
System.out.printf("[ %s ] created, blocked by the latch\n", getName());
|
||||
latch.await();
|
||||
System.out.printf("[ %s ] starts at: %s\n", getName(), Instant.now());
|
||||
// do actual work here...
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.threadsstartatsametime;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
|
||||
public class WorkerWithCyclicBarrier extends Thread {
|
||||
private CyclicBarrier barrier;
|
||||
|
||||
public WorkerWithCyclicBarrier(String name, CyclicBarrier barrier) {
|
||||
this.barrier = barrier;
|
||||
this.setName(name);
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
try {
|
||||
System.out.printf("[ %s ] created, blocked by the barrier\n", getName());
|
||||
barrier.await();
|
||||
System.out.printf("[ %s ] starts at: %s\n", getName(), Instant.now());
|
||||
// do actual work here...
|
||||
} catch (InterruptedException | BrokenBarrierException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue