Executor框架+实例

南笙酒味 提交于 2020-03-01 09:30:29

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<Integer[]> {

	/** 待处理的数据 */
	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 ExecutorCompletionService<Integer[]> completionService;

	private int availableProcessors = 0;

	public ConcurrentCalculator() {

		/*
		 * 获取可用的处理器数量,并根据这个数量指定线程池的大小。
		 */
		availableProcessors = populateAvailableProcessors();
		exec = Executors.newFixedThreadPool(availableProcessors);

		completionService = new ExecutorCompletionService<Integer[]>(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<Future<Integer[]>> tasks = new ArrayList<Future<Integer[]>>();

	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<Integer[]> task = new FutureTask<Integer[]>(
					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<Integer[]> 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();
	}
}

测试

测试包括了三部分: 

  1. 没有用Executor框架,用Arrays.sort直接计算,并从后往前取100个数。 
  2. 使用CompletionService计算结果 
  3. 使用CallableFuture计算结果 
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 100
top1 = 9998
top2 = 9995
top3 = 9994
top4 = 9989
top5 = 9989
top6 = 9989
top7 = 9987
top8 = 9985
top9 = 9984
top10 = 9982
top11 = 9982
top12 = 9981
top13 = 9979
top14 = 9978
top15 = 9977
top16 = 9975
top17 = 9974
top18 = 9974
top19 = 9973
top20 = 9973
top21 = 9971
top22 = 9971
top23 = 9970
top24 = 9970
top25 = 9970
top26 = 9969
top27 = 9969
top28 = 9967
top29 = 9966
top30 = 9965
top31 = 9965
top32 = 9965
top33 = 9963
top34 = 9963
top35 = 9962
top36 = 9961
top37 = 9960
top38 = 9960
top39 = 9960
top40 = 9960
top41 = 9959
top42 = 9958
top43 = 9956
top44 = 9955
top45 = 9954
top46 = 9952
top47 = 9951
top48 = 9950
top49 = 9948
top50 = 9948
top51 = 9945
top52 = 9944
top53 = 9944
top54 = 9939
top55 = 9939
top56 = 9938
top57 = 9936
top58 = 9936
top59 = 9936
top60 = 9935
top61 = 9933
top62 = 9933
top63 = 9932
top64 = 9929
top65 = 9929
top66 = 9925
top67 = 9924
top68 = 9923
top69 = 9923
top70 = 9922
top71 = 9921
top72 = 9921
top73 = 9919
top74 = 9919
top75 = 9918
top76 = 9913
top77 = 9912
top78 = 9911
top79 = 9911
top80 = 9911
top81 = 9908
top82 = 9906
top83 = 9905
top84 = 9905
top85 = 9904
top86 = 9904
top87 = 9903
top88 = 9901
top89 = 9901
top90 = 9900
top91 = 9900
top92 = 9899
top93 = 9899
top94 = 9898
top95 = 9898
top96 = 9897
top97 = 9896
top98 = 9895
top99 = 9892
top100 = 9892
0.067


使用ExecutorCompletionService获取top 100
top1 = 9998
top2 = 9995
top3 = 9994
top4 = 9989
top5 = 9989
top6 = 9989
top7 = 9987
top8 = 9985
top9 = 9984
top10 = 9982
top11 = 9982
top12 = 9981
top13 = 9979
top14 = 9978
top15 = 9977
top16 = 9975
top17 = 9974
top18 = 9974
top19 = 9973
top20 = 9973
top21 = 9971
top22 = 9971
top23 = 9970
top24 = 9970
top25 = 9970
top26 = 9969
top27 = 9969
top28 = 9967
top29 = 9966
top30 = 9965
top31 = 9965
top32 = 9965
top33 = 9963
top34 = 9963
top35 = 9962
top36 = 9961
top37 = 9960
top38 = 9960
top39 = 9960
top40 = 9960
top41 = 9959
top42 = 9958
top43 = 9956
top44 = 9955
top45 = 9954
top46 = 9952
top47 = 9951
top48 = 9950
top49 = 9948
top50 = 9948
top51 = 9945
top52 = 9944
top53 = 9944
top54 = 9939
top55 = 9939
top56 = 9938
top57 = 9936
top58 = 9936
top59 = 9936
top60 = 9935
top61 = 9933
top62 = 9933
top63 = 9932
top64 = 9929
top65 = 9929
top66 = 9925
top67 = 9924
top68 = 9923
top69 = 9923
top70 = 9922
top71 = 9921
top72 = 9921
top73 = 9919
top74 = 9919
top75 = 9918
top76 = 9913
top77 = 9912
top78 = 9911
top79 = 9911
top80 = 9911
top81 = 9908
top82 = 9906
top83 = 9905
top84 = 9905
top85 = 9904
top86 = 9904
top87 = 9903
top88 = 9901
top89 = 9901
top90 = 9900
top91 = 9900
top92 = 9899
top93 = 9899
top94 = 9898
top95 = 9898
top96 = 9897
top97 = 9896
top98 = 9895
top99 = 9892
top100 = 9892
0.025


使用FutureTask 获取top 100
top1 = 9998
top2 = 9995
top3 = 9994
top4 = 9989
top5 = 9989
top6 = 9989
top7 = 9987
top8 = 9985
top9 = 9984
top10 = 9982
top11 = 9982
top12 = 9981
top13 = 9979
top14 = 9978
top15 = 9977
top16 = 9975
top17 = 9974
top18 = 9974
top19 = 9973
top20 = 9973
top21 = 9971
top22 = 9971
top23 = 9970
top24 = 9970
top25 = 9970
top26 = 9969
top27 = 9969
top28 = 9967
top29 = 9966
top30 = 9965
top31 = 9965
top32 = 9965
top33 = 9963
top34 = 9963
top35 = 9962
top36 = 9961
top37 = 9960
top38 = 9960
top39 = 9960
top40 = 9960
top41 = 9959
top42 = 9958
top43 = 9956
top44 = 9955
top45 = 9954
top46 = 9952
top47 = 9951
top48 = 9950
top49 = 9948
top50 = 9948
top51 = 9945
top52 = 9944
top53 = 9944
top54 = 9939
top55 = 9939
top56 = 9938
top57 = 9936
top58 = 9936
top59 = 9936
top60 = 9935
top61 = 9933
top62 = 9933
top63 = 9932
top64 = 9929
top65 = 9929
top66 = 9925
top67 = 9924
top68 = 9923
top69 = 9923
top70 = 9922
top71 = 9921
top72 = 9921
top73 = 9919
top74 = 9919
top75 = 9918
top76 = 9913
top77 = 9912
top78 = 9911
top79 = 9911
top80 = 9911
top81 = 9908
top82 = 9906
top83 = 9905
top84 = 9905
top85 = 9904
top86 = 9904
top87 = 9903
top88 = 9901
top89 = 9901
top90 = 9900
top91 = 9900
top92 = 9899
top93 = 9899
top94 = 9898
top95 = 9898
top96 = 9897
top97 = 9896
top98 = 9895
top99 = 9892
top100 = 9892
0.013

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!