假如有Thread1、Thread2、Thread3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?

实现1:用concurrent包下的CountDownLatch

/**
 * 假如有Thread1、Thread2、Thread3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?
 * 本例,用CountDownLatch实现,CountDownLatch相当于一个计时器
 * */

public class Test1_1 {
	
	public static void main(String[] args) throws InterruptedException {
		final CountDownLatch countDownLatch= new CountDownLatch(4);
		Runnable run1= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("统计C盘");
					countDownLatch.countDown();//单任务,把计数器减1
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		Runnable run2= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("统计D盘");
					countDownLatch.countDown();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		Runnable run3= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("统计E盘");
					countDownLatch.countDown();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		Runnable run4= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("统计F盘");
					countDownLatch.countDown();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		
		ExecutorService service= Executors.newFixedThreadPool(4);
		service.submit(run1);
		service.submit(run2);
		service.submit(run3);
		service.submit(run4);
		/*new Thread(run1).start();
		new Thread(run2).start();
		new Thread(run3).start();
		new Thread(run4).start();*/
		
		countDownLatch.await();//主线程,即第5线程等待
		System.out.println("合计C,D,E,F");
		service.shutdown();

实现2:用concurrent包下的CyclicBarrier

/**
 * 假如有Thread1、Thread2、Thread3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?
 * 本例,用CyclicBarrier实现,CyclicBarrier相当于一个栅栏,会将线程挡住
 * */

public class Test1_2 {
	public static void main(String[] args) {
		Runnable barrierAction= new Runnable() {
			
			@Override
			public void run() {
				System.out.println("统计C,D,E,F盘");
			}
		};
		
		final CyclicBarrier cyclicBarrier= new CyclicBarrier(4, barrierAction );
		Runnable run1= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("C盘");
					cyclicBarrier.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
				
			}
		};
		Runnable run2= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("D盘");
					cyclicBarrier.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
				
			}
		};
		Runnable run3= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("E盘");
					cyclicBarrier.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
				
			}
		};
		Runnable run4= new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("F盘");
					cyclicBarrier.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
				
			}
		};
		
		ExecutorService service= Executors .newFixedThreadPool(4);
		service.submit(run1);
		service.submit(run2);
		service.submit(run3);
		service.submit(run4);
		service.shutdown();
		
		
	}
}

纯手打,如有问题,欢迎指正

posted on 2017-07-10 10:36  WesTward  阅读(951)  评论(0编辑  收藏  举报