AQS框架实现类学习

CountDownLatch:允许一个或者多个线程等待其他线程完成操作

/**
 * 

*

* @author jinxindong 2016年11月1日 上午9:33:46 * @version V1.0 */ public class CountDownLatchDemo { final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(2);//计数数量 Worker worker1 = new Worker("zhang san", 5000, countDownLatch); Worker worker2 = new Worker("li si", 8000, countDownLatch); worker1.start();// worker2.start();// countDownLatch.await();//如果计数到达零,则释放所有等待的线程。 System.out.println("all work done at " + sdf.format(new Date())); } static class Worker extends Thread { String workerName; int workTime; CountDownLatch countDownLatch; public Worker(String workerName, int workTime, CountDownLatch countDownLatch) { this.countDownLatch = countDownLatch; this.workerName = workerName; this.workTime = workTime; } public void run() { try { System.out.println("Worker " + workerName + " do work begin at " + sdf.format(new Date())); Thread.sleep(workTime);// 工作了 System.out.println("Worker " + workerName + " do work complete at " + sdf.format(new Date())); } catch (InterruptedException e) { e.printStackTrace(); } finally { countDownLatch.countDown();//递减锁存器的计数。 } } } }



cyclicBarrier 同步屏障:
拦截到所有后,释放,所有被拦截的线程才会继续进行


cyclicBarrier和CountDownLatch的区别:
countDownLatch只能用一次,cyclicBarrier的计数器可以使用reset()方法重置。


Semaphore:同事允许几个线程并发操作共享资源  方法: acquire() 和release()

/**
 * 

* @author jinxindong 2016年10月31日 下午4:42:07 * @version V1.0 */ //创建一个会实现print queue的类名为PrintQueue public class PrintQueue { private final Semaphore semaphore;//声明对象为一个semaphore 信号量 public PrintQueue(){ semaphore = new Semaphore(2); } //实现打印功能 模拟打印文档 public void printJob(Object document){ try { semaphore.acquire(); long duration = (long)(Math.random()*10); System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),duration); Thread.sleep(duration); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ semaphore.release(); } } }

Exchanger:两个线程间,的数据交换。如果多余两个取前两个。执行exchange后,返回值是另一个线程的结果。


/**
 * 

* @author jinxindong 2016年11月1日 上午10:29:45 * @version V1.0 */ public class ExchangerTest { private static final Exchanger exc = new Exchanger(); private static ExecutorService executorService = Executors.newFixedThreadPool(2); /** * @author jinxindong 2016年11月1日 上午10:29:45 * @param args */ public static void main(String[] args) { //执行线程A executorService.execute(new Runnable() { @Override public void run() { try { String A = "银行流水A"; String b = exc.exchange(A); System.out.println(" b= "+b); } catch (Exception e) { e.printStackTrace(); } } }); //执行线程B executorService.execute(new Runnable() { @Override public void run() { try { String B = "银行流水B"; String a = exc.exchange(B); System.out.println(" a= "+a); } catch (InterruptedException e) { e.printStackTrace(); } } }); executorService.shutdown(); } }


Phaser:
可以代替countDownLatch和CyclicBarrier

1  Phaser pha = new Phaser(2);//countDownLatch(2)
    pha.arrive() // latch.countDown() 减1
    pha.awaitAdvance // latch.await()

2   Phaser pha = new Phaser(2);
     pha.arriveAndAwaitAdvance() // cyc.await()


你可能感兴趣的:(多线程并发)