Java并发编程ITeye - 牛牛娱乐

Java并发编程ITeye

2019年02月19日11时43分48秒 | 作者: 浩广 | 标签: 线程,数组,数据 | 浏览: 1290


Executor结构是指java 5中引进的一系列并发库中与executor相关的一些功用类,其间包含线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。运用该结构能够很好的将使命分红一个个的子使命,使并发编程变得便利。该结构的类图(办法并没有都表示出来)如下:





创立线程池的介绍,摘自http://mshijie.iteye.com/blog/366591
创立线程池
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结构来完结一个使命:求出10000个随机数据中的top 100.

Note:本文仅仅用Executor来做一个比如,并不是用最好的办法去求10000个数中最大的100个数。

详细的完结如下:
1. 随机发生10000个数(规模1~9999),并存放在一个文件中。
2. 读取该文件的数值,并存放在一个数组中。
3. 选用Executor结构,进行并发操作,将10000个数据用10个线程来做,每个线程完结1000=(10000/10)个数据的top 100操作。
4. 将10个线程回来的各个top 100数据,从头核算,得出最终的10000个数据的top 100.


随机发生数和读取随机数文件的类如下:

package my.concurrent.demo;
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;
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();
 } finally {
 bw = null;
 * 从指定文件中提取现已发生的随机数集
 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();
 } finally {
 br = null;
 return values;
}



编写一个Calculator 类, 完结Callable接口,核算指定数据集规模内的top 100.

package my.concurrent.demo;
import java.util.Arrays;
import java.util.concurrent.Callable;
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完结

package my.concurrent.demo;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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 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 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核算成果
package my.concurrent.demo;
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;
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 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. 运用Callable和Future核算成果

测验代码如下:
package my.concurrent.demo;
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]));
}


测验成果如下:



版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章