之前的文章中学习了J.U.C中aqs的底层实现原理,这篇文学习一下J.U.C中提供的一些线程同步工具类。
ConditionWait public class ConditionDemoWait implements Runnable { private Lock lock; private Condition condition; public ConditionDemoWait(Lock lock, Condition condition) { this.lock = lock; this.condition = condition; } @Override public void run() { System.out.println("begin - ConditionDemoWait"); try { lock.lock(); condition.await(); System.out.println("end - ConditionDemoWait"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }ConditionSignal public class ConditionDemoSignal implements Runnable { private Lock lock; private Condition condition; public ConditionDemoSignal(Lock lock, Condition condition) { this.lock = lock; this.condition = condition; } @Override public void run() { System.out.println("begin - ConditionDemoSignal"); try { lock.lock(); condition.signal(); System.out.println("end - ConditionDemoSignal"); } finally { lock.unlock(); } } }
public static void main(String[] args) throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(3); new Thread(() -> { System.out.println("" + Thread.currentThread().g etName() + "-执行中"); countDownLatch.countDown(); System.out.println("" + Thread.currentThread().g etName() + "-执行完毕"); }, "t1").start(); new Thread(() -> { System.out.println("" + Thread.currentThread().g etName() + "-执行中"); countDownLatch.countDown(); System.out.println("" + Thread.currentThread().g etName() + "-执行完毕"); }, "t2").start(); new Thread(() -> { System.out.println("" + Thread.currentThread().g etName() + "-执行中"); countDownLatch.countDown(); System.out.println("" + Thread.currentThread().g etName() + "-执行完毕"); }, "t3").start(); countDownLatch.await(); System.out.println("所有线程执行完毕"); }
public class Test { public static void main(String[] args) { Semaphore semaphore = new Semaphore(5); for (int i = 0; i < 10; i++) { new Car(i, semaphore).start(); } } static class Car extends Thread { private int num; private Semaphore semaphore; public Car(int num, Semaphore semaphore) { this.num = num; this.semaphore = semaphore; } public void run() { try { semaphore.acquire();//获取一个许可 System.out.println("第" + num + "占用一个停车位"); TimeUnit.SECONDS.sleep(2); System.out.println("第" + num + "俩车走喽"); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public class DataImportThread extends Thread { private CyclicBarrier cyclicBarrier; private String path; public DataImportThread(CyclicBarrier cyclicBarrier, String path) { this.cyclicBarrier = cyclicBarrier; this.path = path; } @Override public void run() { System.out.println("开始导入:"+path+" 位置的数据"); try { cyclicBarrier.await();//阻塞 } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
public class CycliBarrierDemo extends Thread {
@Override
public void run() {
System.out.println("开始进行数据分析");
}
public static void main(String[] args) {
CyclicBarrier cycliBarrier = new CyclicBarrier(3, new CycliBarrierDemo());
new Thread(new DataImportThread(cycliBarrier, "file1")).start();
new Thread(new DataImportThread(cycliBarrier, "file2")).start();
new Thread(new DataImportThread(cycliBarrier, "file3")).start();
}
}
原文:https://www.cnblogs.com/talkingcat/p/13491134.html