Executor框架+實例

Executor框架是指java 5中引入的一系列併發庫中與executor相關的一些功能類,其中包括線程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。java

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類。 dom

Executor框架使用案例

案例描述

本文主要是使用Executor框架來完成一個任務:求出10000個隨機數據中的top 100。

Note:測試

本文只是用Executor來作一個例子,並非用最好的辦法去求10000個數中最大的100個數。 this

過程描述

具體過程有以下4個步驟組成:spa

  • 隨機產生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
相關文章
相關標籤/搜索