zoukankan      html  css  js  c++  java
  • java 线程 新类库中的构件 countDownLatch 使用

    package org.rui.thread.newc;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    /**
     * Latch 锁存器 
     * 新类库中的构件 countDownLatch
     * 
     * @author lenovo
     * 
     */
    class TaskPortion implements Runnable {
    
    	private static int counter = 0;
    	private final int id = counter++;
    	private static Random rand = new Random(47);
    
    	private final CountDownLatch latch;
    
    	public TaskPortion(CountDownLatch latch) {
    		this.latch = latch;
    	}
    
    	@Override
    	public void run() {
    		try {
    			doWork();
    			latch.countDown();
    		} catch (InterruptedException e) {
    			// acceptable way to exit
    		}
    
    	}
    
    	// 处理业务代码
    	public void doWork() throws InterruptedException {
    		TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000));
    		System.out.println(this + " 完毕");
    	}
    
    	public String toString() {
    		return String.format("%1$-3d", id);
    	}
    
    }
    
    // waits on the countDownLatch
    class WaitingTask implements Runnable {
    	private static int counter = 0;// 计数
    	private final int id = counter++;
    	private static Random rand = new Random(47);
    
    	private final CountDownLatch latch;
    
    	WaitingTask(CountDownLatch latch) {
    		this.latch = latch;
    	}
    
    	@Override
    	public void run() {
    		try {
    			// 调用countDown()的任务在产生调用时并没有被堵塞。仅仅有对await的调用会被堵塞,直至计数值到达0
    			// 等待问题被解决的任务在这个锁存器上调用await(),将它们自已拦住,直至锁存器计数结束
    			latch.await();
    			System.out.println("latch 障碍被觉得 " + this);
    		} catch (InterruptedException e) {
    			System.out.println(this + " interrupted");
    		}
    	}
    
    	public String toString() {
    		return String.format("waitingTask %1$-3d", id);
    	}
    }
    
    /**
     * TaskPortio将随机地休眠一段时间,以模拟这部分工作的完毕,而WaitingTask表示系统中等待的部分。它要等待到问题的初始部分成完为止。
     * 全部的任务都使用了在main中定义同一个单一的counDownLacth
     * 
     * @author lenovo
     * 
     */
    public class CountDownLatchDemo {
    	static final int SIZE = 100;
    
    	public static void main(String[] args) throws InterruptedException {
    		ExecutorService exec = Executors.newCachedThreadPool();
    		CountDownLatch latch = new CountDownLatch(SIZE);
    
    		// 都必须共享一个countDownLatch对象
    		for (int i = 0; i < 10; i++) {
    			exec.execute(new WaitingTask(latch));// 这个要等待 latch上面的为0时才会运行
    		}
    		for (int i = 0; i < SIZE; i++) {
    			exec.execute(new TaskPortion(latch));
    		}
    
    		// latch.await();
    		System.out.println("launched all tasks");
    		exec.shutdown();// quit when all task complete
    	}
    
    }
    /**
    output:
    launched all tasks
    43  完毕
    95  完毕
    99  完毕
    36  完毕
    94  完毕
    11  完毕
    ....
    12  完毕
    1   完毕
    27  完毕
    98  完毕
    13  完毕
    72  完毕
    71  完毕
    2   完毕
    45  完毕
    92  完毕
    31  完毕
    14  完毕
    17  完毕
    6   完毕
    97  完毕
    ....
    80  完毕
    ....
    56  完毕
    85  完毕
    61  完毕
    30  完毕
    ....
    3   完毕
    93  完毕
    81  完毕
    78  完毕
    73  完毕
    44  完毕
    82  完毕
    49  完毕
    64  完毕
    83  完毕
    16  完毕
    latch 障碍被觉得 waitingTask 2  
    latch 障碍被觉得 waitingTask 0  
    latch 障碍被觉得 waitingTask 4  
    latch 障碍被觉得 waitingTask 1  
    latch 障碍被觉得 waitingTask 5  
    latch 障碍被觉得 waitingTask 3  
    latch 障碍被觉得 waitingTask 7  
    latch 障碍被觉得 waitingTask 6  
    latch 障碍被觉得 waitingTask 9  
    latch 障碍被觉得 waitingTask 8  
    */


  • 相关阅读:
    Sql的基础知识(一)
    Django--分页功能
    django--基础操作
    CSRF攻击与防御(转载)
    谈谈CSRF
    浅谈Dom遍历
    node50行代码实现壁纸爬取
    node解决request中文乱码问题
    数组去重方法(转载)
    淘宝dns解析错误导致首页打不开
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5103904.html
Copyright © 2011-2022 走看看