package com.stock.test.base.thread; import org.junit.Test; import java.io.IOException; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * * 并发工具包:java.util.concurrent 实例 * CyclicBarrier cyclic /'saɪklɪk/ adj. 环的;循环的;周期的 * Barrier /'bærɪɚ/ n. 障碍物,屏障;界线 ; vt. 把…关入栅栏 * * CountDownLatch * */ public class ConcurrentUtilTest { /** * CyclicBarrier 循环界限 * 使多个线程在同一时间同时执行 * @throws IOException */ @Test public void cyclicBarrier() throws InterruptedException { CyclicBarrier cyclicBarrier = new CyclicBarrier(4); ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); ReentrantReadWriteLock.ReadLock readLock = lock.readLock(); ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); new Thread(new CyclicBarrierThread("线程One",cyclicBarrier,readLock)).start(); new Thread(new CyclicBarrierThread("线程Two",cyclicBarrier,readLock)).start(); new Thread(new CyclicBarrierThread("线程Three",cyclicBarrier,readLock)).start(); new Thread(new CyclicBarrierThread("线程Four",cyclicBarrier,readLock)).start(); // 提交4个线程之后,被提交的4个线程同事执行 Thread.sleep(10000); } public class CyclicBarrierThread implements Runnable{ private String name; private CyclicBarrier cyclicBarrier; private ReentrantReadWriteLock.ReadLock readLock; public CyclicBarrierThread(String name, CyclicBarrier cyclicBarrier, ReentrantReadWriteLock.ReadLock readLock){ this.name = name; this.cyclicBarrier = cyclicBarrier; this.readLock = readLock; } @Override public void run() { try { System.out.println(this.name+"--->准备OK"); cyclicBarrier.await(); readLock.lock(); Thread.sleep(2000); System.out.println(this.name+"--->GO"); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); }finally { readLock.unlock(); } } } /** * 一个线程等待多个线程通知继续执行 * @throws InterruptedException */ @Test public void countDownLatch() throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(2); new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"--->执行"); try { countDownLatch.await(); System.out.println("终于结束了"); } catch (InterruptedException e) { e.printStackTrace(); } } },"线程---1").start(); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+"--->执行"); countDownLatch.countDown(); System.out.println(Thread.currentThread().getName()+"--->执行通知了"); } catch (InterruptedException e) { e.printStackTrace(); } } },"线程---2").start(); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); System.out.println(Thread.currentThread().getName()+"--->执行"); countDownLatch.countDown(); System.out.println(Thread.currentThread().getName()+"--->执行通知了"); } catch (InterruptedException e) { e.printStackTrace(); } } },"线程---3").start(); Thread.sleep(100000000); } public class CountDownLatchThread implements Runnable{ private String name; public CountDownLatchThread(String name){ this.name = name; } @Override public void run() { try { System.out.println(this.name+"--->开始执行"); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
3、线程之间通信
最新推荐文章于 2024-09-12 23:58:52 发布