CountDownLatch的API讲解
1.使用executor.awaitTermination(1, TimeUnit.HOURS);
package com.dwz.utils; 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; public class CountDownLatchExample1 { private static Random random = new Random(System.currentTimeMillis()); private static ExecutorService executor = Executors.newFixedThreadPool(2); private static final CountDownLatch countDownLatch = new CountDownLatch(11); public static void main(String[] args) throws InterruptedException { //(1)查询数据 int[] data = query(); //(2)并行处理数据 for(int i = 0; i < data.length; i++) { executor.execute(new SimpleRunnable(data, i)); } //(3) executor.shutdown(); executor.awaitTermination(1, TimeUnit.HOURS); System.out.println("all of work finish done."); } static class SimpleRunnable implements Runnable { private final int[] data; private final int index; public SimpleRunnable(int[] data, int index) { this.data = data; this.index = index; } @Override public void run() { try { Thread.sleep(random.nextInt(2000)); } catch (InterruptedException e) { e.printStackTrace(); } int value = data[index]; if(value % 2 == 0) { data[index] = value * 2; } else { data[index] = value * 10; } System.out.println(Thread.currentThread().getName() + " finished."); } } private static int[] query() { return new int[] {0,1,2,3,4,5,6,7,8,9,10}; } }
2.使用countDownLatch.await();
package com.dwz.utils; 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; public class CountDownLatchExample1 { private static Random random = new Random(System.currentTimeMillis()); private static ExecutorService executor = Executors.newFixedThreadPool(2); private static final CountDownLatch countDownLatch = new CountDownLatch(11); public static void main(String[] args) throws InterruptedException { //(1)查询数据 int[] data = query(); //(2)并行处理数据 for(int i = 0; i < data.length; i++) { executor.execute(new SimpleRunnable(data, i)); } //(3) countDownLatch.await(); System.out.println("all of work finish done."); } static class SimpleRunnable implements Runnable { private final int[] data; private final int index; public SimpleRunnable(int[] data, int index) { this.data = data; this.index = index; } @Override public void run() { try { Thread.sleep(random.nextInt(2000)); } catch (InterruptedException e) { e.printStackTrace(); } int value = data[index]; if(value % 2 == 0) { data[index] = value * 2; } else { data[index] = value * 10; } countDownLatch.countDown(); System.out.println(Thread.currentThread().getName() + " finished."); } } private static int[] query() { return new int[] {0,1,2,3,4,5,6,7,8,9,10}; } }
3.latch.await();和latch.countDown();的混合使用
package com.dwz.utils; import java.util.concurrent.CountDownLatch; public class CountDownLatchExample2 { public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(1); new Thread() { @Override public void run() { System.out.println("Do some initial work."); try { Thread.sleep(1000); latch.await(); System.out.println("Do other work."); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); new Thread() { @Override public void run() { System.out.println("asyn prepare for some data."); try { Thread.sleep(2000); System.out.println("data prepare done."); } catch (InterruptedException e) { e.printStackTrace(); } finally { latch.countDown(); } } }.start(); } }