Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。
Executor框架简介
Executor框架描述
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。
运用该框架能够很好的将任务分成一个个的子任务,使并发编程变得方便。
Executor相关类图
该框架的类图(方法并没有都表示出来)如下:
创建线程池类别
- 创建线程池
Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
public static ExecutorService newFixedThreadPool(int nThreads)
- 创建固定数目线程的线程池
public static ExecutorService newCachedThreadPool()
- 创建一个可缓存的线程池
创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。
public static ExecutorService newSingleThreadExecutor()
- 创建一个单线程化的Executor
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。
Executor框架使用案例
案例描述
本文主要是使用Executor框架来完成一个任务:求出10000个随机数据中的top 100。
Note:本文只是用Executor来做一个例子,并不是用最好的办法去求10000个数中最大的100个数。
过程描述
具体过程有如下4个步骤组成:
- 随机产生10000个数(范围1~9999),并存放在一个文件中。
- 读取该文件的数值,并存放在一个数组中。
- 采用Executor框架,进行并发操作,将10000个数据用10个线程来做,每个线程完成1000=(10000/10)个数据的top 100操作。
- 将10个线程返回的各个top 100数据,重新计算,得出最后的10000个数据的top 100。
代码思考和实现
随机产生数和读取随机数文件的类如下:
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Random;/** * * @author wangmengjun * */public class RandomUtil { private static final int RANDOM_SEED = 10000; private static final int SIZE = 10000; /** * 产生10000万个随机数(范围1~9999),并将这些数据添加到指定文件中去。 * * 例如: * * 1=7016 2=7414 3=3117 4=6711 5=5569 ... ... 9993=1503 9994=9528 9995=9498 * 9996=9123 9997=6632 9998=8801 9999=9705 10000=2900 */ public static void generatedRandomNbrs(String filepath) { Random random = new Random(); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(new File(filepath))); for (int i = 0; i < SIZE; i++) { bw.write((i + 1) + "=" + random.nextInt(RANDOM_SEED)); bw.newLine(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != bw) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 从指定文件中提取已经产生的随机数集 */ public static int[] populateValuesFromFile(String filepath) { BufferedReader br = null; int[] values = new int[SIZE]; try { br = new BufferedReader(new FileReader(new File(filepath))); int count = 0; String line = null; while (null != (line = br.readLine())) { values[count++] = Integer.parseInt(line.substring(line .indexOf("=") + 1)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return values; }}
编写一个Calculator 类, 实现Callable接口,计算指定数据集范围内的top 100。
import java.util.Arrays;import java.util.concurrent.Callable;/** * * @author wangmengjun * */public class Calculator implements Callable{ /** 待处理的数据 */ private int[] values; /** 起始索引 */ private int startIndex; /** 结束索引 */ private int endIndex; /** * @param values * @param startIndex * @param endIndex */ public Calculator(int[] values, int startIndex, int endIndex) { this.values = values; this.startIndex = startIndex; this.endIndex = endIndex; } public Integer[] call() throws Exception { // 将指定范围的数据复制到指定的数组中去 int[] subValues = new int[endIndex - startIndex + 1]; System.arraycopy(values, startIndex, subValues, 0, endIndex - startIndex + 1); Arrays.sort(subValues); // 将排序后的是数组数据,取出top 100 并返回。 Integer[] top100 = new Integer[100]; for (int i = 0; i < 100; i++) { top100[i] = subValues[subValues.length - i - 1]; } return top100; } /** * @return the values */ public int[] getValues() { return values; } /** * @param values * the values to set */ public void setValues(int[] values) { this.values = values; } /** * @return the startIndex */ public int getStartIndex() { return startIndex; } /** * @param startIndex * the startIndex to set */ public void setStartIndex(int startIndex) { this.startIndex = startIndex; } /** * @return the endIndex */ public int getEndIndex() { return endIndex; } /** * @param endIndex * the endIndex to set */ public void setEndIndex(int endIndex) { this.endIndex = endIndex; }}
使用CompletionService实现
import java.util.Arrays;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorCompletionService;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * * @author wangmengjun * */public class ConcurrentCalculator { private ExecutorService exec; private ExecutorCompletionServicecompletionService; private int availableProcessors = 0; public ConcurrentCalculator() { /* * 获取可用的处理器数量,并根据这个数量指定线程池的大小。 */ availableProcessors = populateAvailableProcessors(); exec = Executors.newFixedThreadPool(availableProcessors); completionService = new ExecutorCompletionService (exec); } /** * 获取10000个随机数中top 100的数。 */ public Integer[] top100(int[] values) { /* * 用十个线程,每个线程处理1000个。 */ for (int i = 0; i < 10; i++) { completionService.submit(new Calculator(values, i * 1000, i * 1000 + 1000 - 1)); } shutdown(); return populateTop100(); } /** * 计算top 100的数。 * * 计算方法如下: 1. 初始化一个top 100的数组,数值都为0,作为当前的top 100. 2. 将这个当前的top * 100数组依次与每个线程产生的top 100数组比较,调整当前top 100的值。 * */ private Integer[] populateTop100() { Integer[] top100 = new Integer[100]; for (int i = 0; i < 100; i++) { top100[i] = new Integer(0); } for (int i = 0; i < 10; i++) { try { adjustTop100(top100, completionService.take().get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } return top100; } /** * 将当前top 100数组和一个线程返回的top 100数组比较,并调整当前top 100数组的数据。 */ private void adjustTop100(Integer[] currentTop100, Integer[] subTop100) { Integer[] currentTop200 = new Integer[200]; System.arraycopy(currentTop100, 0, currentTop200, 0, 100); System.arraycopy(subTop100, 0, currentTop200, 100, 100); Arrays.sort(currentTop200); for (int i = 0; i < currentTop100.length; i++) { currentTop100[i] = currentTop200[currentTop200.length - i - 1]; } } /** * 关闭 executor */ public void shutdown() { exec.shutdown(); } /** * 返回可以用的处理器个数 */ private int populateAvailableProcessors() { return Runtime.getRuntime().availableProcessors(); }}
使用Callable,Future计算结果
import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.FutureTask;/** * * @author wangmengjun * */public class ConcurrentCalculator2 { private List> tasks = new ArrayList >(); private ExecutorService exec; private int availableProcessors = 0; public ConcurrentCalculator2() { /* * 获取可用的处理器数量,并根据这个数量指定线程池的大小。 */ availableProcessors = populateAvailableProcessors(); exec = Executors.newFixedThreadPool(availableProcessors); } /** * 获取10000个随机数中top 100的数。 */ public Integer[] top100(int[] values) { /* * 用十个线程,每个线程处理1000个。 */ for (int i = 0; i < 10; i++) { FutureTask task = new FutureTask ( new Calculator(values, i * 1000, i * 1000 + 1000 - 1)); tasks.add(task); if (!exec.isShutdown()) { exec.submit(task); } } shutdown(); return populateTop100(); } /** * 计算top 100的数。 * * 计算方法如下: 1. 初始化一个top 100的数组,数值都为0,作为当前的top 100. 2. 将这个当前的top * 100数组依次与每个Task产生的top 100数组比较,调整当前top 100的值。 * */ private Integer[] populateTop100() { Integer[] top100 = new Integer[100]; for (int i = 0; i < 100; i++) { top100[i] = new Integer(0); } for (Future task : tasks) { try { adjustTop100(top100, task.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return top100; } /** * 将当前top 100数组和一个线程返回的top 100数组比较,并调整当前top 100数组的数据。 */ private void adjustTop100(Integer[] currentTop100, Integer[] subTop100) { Integer[] currentTop200 = new Integer[200]; System.arraycopy(currentTop100, 0, currentTop200, 0, 100); System.arraycopy(subTop100, 0, currentTop200, 100, 100); Arrays.sort(currentTop200); for (int i = 0; i < currentTop100.length; i++) { currentTop100[i] = currentTop200[currentTop200.length - i - 1]; } } /** * 关闭executor */ public void shutdown() { exec.shutdown(); } /** * 返回可以用的处理器个数 */ private int populateAvailableProcessors() { return Runtime.getRuntime().availableProcessors(); }}
测试
测试包括了三部分:
- 没有用Executor框架,用Arrays.sort直接计算,并从后往前取100个数。
- 使用CompletionService计算结果
- 使用Callable和Future计算结果
import java.util.Arrays;public class Test { private static final String FILE_PATH = "D:\\RandomNumber.txt"; public static void main(String[] args) { test(); } private static void test() { /** * 如果随机数已经存在文件中,可以不再调用此方法,除非想用新的随机数据。 */ generateRandomNbrs(); process1(); process2(); process3(); } private static void generateRandomNbrs() { RandomUtil.generatedRandomNbrs(FILE_PATH); } private static void process1() { long start = System.currentTimeMillis(); System.out.println("没有使用Executor框架,直接使用Arrays.sort获取top 100"); printTop100(populateTop100(RandomUtil.populateValuesFromFile(FILE_PATH))); long end = System.currentTimeMillis(); System.out.println((end - start) / 1000.0); } private static void process2() { long start = System.currentTimeMillis(); System.out.println("使用ExecutorCompletionService获取top 100"); ConcurrentCalculator calculator = new ConcurrentCalculator(); Integer[] top100 = calculator.top100(RandomUtil .populateValuesFromFile(FILE_PATH)); for (int i = 0; i < top100.length; i++) { System.out.println(String.format("top%d = %d", (i + 1), top100[i])); } long end = System.currentTimeMillis(); System.out.println((end - start) / 1000.0); } private static void process3() { long start = System.currentTimeMillis(); System.out.println("使用FutureTask 获取top 100"); ConcurrentCalculator2 calculator2 = new ConcurrentCalculator2(); Integer[] top100 = calculator2.top100(RandomUtil .populateValuesFromFile(FILE_PATH)); for (int i = 0; i < top100.length; i++) { System.out.println(String.format("top%d = %d", (i + 1), top100[i])); } long end = System.currentTimeMillis(); System.out.println((end - start) / 1000.0); } private static int[] populateTop100(int[] values) { Arrays.sort(values); int[] top100 = new int[100]; int length = values.length; for (int i = 0; i < 100; i++) { top100[i] = values[length - 1 - i]; } return top100; } private static void printTop100(int[] top100) { for (int i = 0; i < top100.length; i++) { System.out.println(String.format("top%d = %d", (i + 1), top100[i])); } }}
某次运行结果如下:
没有使用Executor框架,直接使用Arrays.sort获取top 100top1 = 9998top2 = 9995top3 = 9994top4 = 9989top5 = 9989top6 = 9989top7 = 9987top8 = 9985top9 = 9984top10 = 9982top11 = 9982top12 = 9981top13 = 9979top14 = 9978top15 = 9977top16 = 9975top17 = 9974top18 = 9974top19 = 9973top20 = 9973top21 = 9971top22 = 9971top23 = 9970top24 = 9970top25 = 9970top26 = 9969top27 = 9969top28 = 9967top29 = 9966top30 = 9965top31 = 9965top32 = 9965top33 = 9963top34 = 9963top35 = 9962top36 = 9961top37 = 9960top38 = 9960top39 = 9960top40 = 9960top41 = 9959top42 = 9958top43 = 9956top44 = 9955top45 = 9954top46 = 9952top47 = 9951top48 = 9950top49 = 9948top50 = 9948top51 = 9945top52 = 9944top53 = 9944top54 = 9939top55 = 9939top56 = 9938top57 = 9936top58 = 9936top59 = 9936top60 = 9935top61 = 9933top62 = 9933top63 = 9932top64 = 9929top65 = 9929top66 = 9925top67 = 9924top68 = 9923top69 = 9923top70 = 9922top71 = 9921top72 = 9921top73 = 9919top74 = 9919top75 = 9918top76 = 9913top77 = 9912top78 = 9911top79 = 9911top80 = 9911top81 = 9908top82 = 9906top83 = 9905top84 = 9905top85 = 9904top86 = 9904top87 = 9903top88 = 9901top89 = 9901top90 = 9900top91 = 9900top92 = 9899top93 = 9899top94 = 9898top95 = 9898top96 = 9897top97 = 9896top98 = 9895top99 = 9892top100 = 98920.067使用ExecutorCompletionService获取top 100top1 = 9998top2 = 9995top3 = 9994top4 = 9989top5 = 9989top6 = 9989top7 = 9987top8 = 9985top9 = 9984top10 = 9982top11 = 9982top12 = 9981top13 = 9979top14 = 9978top15 = 9977top16 = 9975top17 = 9974top18 = 9974top19 = 9973top20 = 9973top21 = 9971top22 = 9971top23 = 9970top24 = 9970top25 = 9970top26 = 9969top27 = 9969top28 = 9967top29 = 9966top30 = 9965top31 = 9965top32 = 9965top33 = 9963top34 = 9963top35 = 9962top36 = 9961top37 = 9960top38 = 9960top39 = 9960top40 = 9960top41 = 9959top42 = 9958top43 = 9956top44 = 9955top45 = 9954top46 = 9952top47 = 9951top48 = 9950top49 = 9948top50 = 9948top51 = 9945top52 = 9944top53 = 9944top54 = 9939top55 = 9939top56 = 9938top57 = 9936top58 = 9936top59 = 9936top60 = 9935top61 = 9933top62 = 9933top63 = 9932top64 = 9929top65 = 9929top66 = 9925top67 = 9924top68 = 9923top69 = 9923top70 = 9922top71 = 9921top72 = 9921top73 = 9919top74 = 9919top75 = 9918top76 = 9913top77 = 9912top78 = 9911top79 = 9911top80 = 9911top81 = 9908top82 = 9906top83 = 9905top84 = 9905top85 = 9904top86 = 9904top87 = 9903top88 = 9901top89 = 9901top90 = 9900top91 = 9900top92 = 9899top93 = 9899top94 = 9898top95 = 9898top96 = 9897top97 = 9896top98 = 9895top99 = 9892top100 = 98920.025使用FutureTask 获取top 100top1 = 9998top2 = 9995top3 = 9994top4 = 9989top5 = 9989top6 = 9989top7 = 9987top8 = 9985top9 = 9984top10 = 9982top11 = 9982top12 = 9981top13 = 9979top14 = 9978top15 = 9977top16 = 9975top17 = 9974top18 = 9974top19 = 9973top20 = 9973top21 = 9971top22 = 9971top23 = 9970top24 = 9970top25 = 9970top26 = 9969top27 = 9969top28 = 9967top29 = 9966top30 = 9965top31 = 9965top32 = 9965top33 = 9963top34 = 9963top35 = 9962top36 = 9961top37 = 9960top38 = 9960top39 = 9960top40 = 9960top41 = 9959top42 = 9958top43 = 9956top44 = 9955top45 = 9954top46 = 9952top47 = 9951top48 = 9950top49 = 9948top50 = 9948top51 = 9945top52 = 9944top53 = 9944top54 = 9939top55 = 9939top56 = 9938top57 = 9936top58 = 9936top59 = 9936top60 = 9935top61 = 9933top62 = 9933top63 = 9932top64 = 9929top65 = 9929top66 = 9925top67 = 9924top68 = 9923top69 = 9923top70 = 9922top71 = 9921top72 = 9921top73 = 9919top74 = 9919top75 = 9918top76 = 9913top77 = 9912top78 = 9911top79 = 9911top80 = 9911top81 = 9908top82 = 9906top83 = 9905top84 = 9905top85 = 9904top86 = 9904top87 = 9903top88 = 9901top89 = 9901top90 = 9900top91 = 9900top92 = 9899top93 = 9899top94 = 9898top95 = 9898top96 = 9897top97 = 9896top98 = 9895top99 = 9892top100 = 98920.013