package run;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TheadDemo01 {
public static void main(String[] args) {
int containerNum = 800;
int taskTime = 40;
singleThreadRun(containerNum, taskTime);
manyThreadRun(containerNum, taskTime);
manyThreadRunByCompletionService(containerNum, taskTime);
manyThreadRunByCompletionServiceTest(containerNum, taskTime);
manyThreadRunByCompletionServiceTest2(containerNum, taskTime);
}
/**
* 单线程运行速度
*
* @param containerNum
* @param taskTime
*/
public static void singleThreadRun(int containerNum, int taskTime) {
int result = 0;
long start = System.currentTimeMillis();
for (int i = 0; i < containerNum; i++) {
try {
// 模拟运行任务
Thread.sleep(taskTime);
result += 1;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("Single Thread RunTime : " + (end - start));
System.out.println("Single Thread Result : " + result);
}
/**
* 运行时阻塞多线程
*
* @param containerNum
* @param taskTime
*/
public static void manyThreadRun(int containerNum, int taskTime) {
int cpuNum = Runtime.getRuntime().availableProcessors();
System.out.println(cpuNum);
ThreadPoolExecutor excutor = new ThreadPoolExecutor(cpuNum, cpuNum, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000));
int result = 0;
long start = System.currentTimeMillis();
List<Future<Integer>> futures = new ArrayList<>();
for (int i = 0; i < containerNum; i++) {
Future<Integer> future = excutor.submit(() -> {
// 模拟运行任务
Thread.sleep(taskTime);
return 1;
});
futures.add(future);
}
for (int i = 0; i < containerNum; i++) {
Future<Integer> future = futures.get(i);
try {
// futures.get()会阻塞线程
Integer futureResult = future.get();
result += futureResult;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
excutor.shutdown();
long end = System.currentTimeMillis();
System.out.println("Many Thread RunTime : " + (end - start));
System.out.println("Many Thread Result : " + result);
}
/**
* 运行结果阻塞多线程
*
* @param containerNum
* @param taskTime
*/
public static void manyThreadRunByCompletionService(int containerNum, int taskTime) {
int cpuNum = Runtime.getRuntime().availableProcessors();
System.out.println(cpuNum);
ThreadPoolExecutor excutor = new ThreadPoolExecutor(cpuNum, cpuNum, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000));
CompletionService<Integer> completionService = new ExecutorCompletionService<>(excutor);
int result = 0;
long start = System.currentTimeMillis();
List<Future<Integer>> futures = new ArrayList<>();
for (int i = 0; i < containerNum; i++) {
Future<Integer> future = completionService.submit(() -> {
// 模拟运行任务
Thread.sleep(taskTime);
return 1;
});
futures.add(future);
}
for (int i = 0; i < containerNum; i++) {
try {
Future<Integer> future = completionService.take();
Integer futureResult = future.get();
result += futureResult;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
excutor.shutdown();
System.out.println("CompletionService Many Thread RunTime : " + (end - start));
System.out.println("CompletionService Many Thread Result : " + result);
}
// 问:测试1和测试2的区别在哪?
/**
* 测试1
*
* @param containerNum
* @param taskTime
*/
public static void manyThreadRunByCompletionServiceTest(int containerNum, int taskTime) {
int cpuNum = Runtime.getRuntime().availableProcessors();
System.out.println(cpuNum);
ThreadPoolExecutor excutor = new ThreadPoolExecutor(cpuNum, cpuNum, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000));
CompletionService<Integer> completionService = new ExecutorCompletionService<>(excutor);
int result = 0;
long start = System.currentTimeMillis();
for (int i = 0; i < containerNum; i++) {
List<Future<Integer>> futures = new ArrayList<>();
for (int j = 0; j < 1; j++) {
Future<Integer> future = completionService.submit(() -> {
// 模拟运行任务
Thread.sleep(taskTime);
return 1;
});
futures.add(future);
}
for (int j = 0; j < futures.size(); j++) {
try {
Future<Integer> future = completionService.take();
Integer futureResult = future.get();
result += futureResult;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
excutor.shutdown();
System.out.println("CompletionService Test Many Thread RunTime : " + (end - start));
System.out.println("CompletionService Test Many Thread Result : " + result);
}
/**
* 测试2
*
* @param containerNum
* @param taskTime
*/
public static void manyThreadRunByCompletionServiceTest2(int containerNum, int taskTime) {
int cpuNum = Runtime.getRuntime().availableProcessors();
System.out.println(cpuNum);
ThreadPoolExecutor excutor = new ThreadPoolExecutor(cpuNum, cpuNum, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000));
CompletionService<Integer> completionService = new ExecutorCompletionService<>(excutor);
int result = 0;
long start = System.currentTimeMillis();
List<Future<Integer>> futures = new ArrayList<>();
for (int i = 0; i < containerNum; i++) {
for (int j = 0; j < 1; j++) {
Future<Integer> future = completionService.submit(() -> {
// 模拟运行任务
Thread.sleep(taskTime);
return 1;
});
futures.add(future);
}
}
for (int j = 0; j < futures.size(); j++) {
try {
Future<Integer> future = completionService.take();
Integer futureResult = future.get();
result += futureResult;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
excutor.shutdown();
System.out.println("CompletionService Test2 Many Thread RunTime : " + (end - start));
System.out.println("CompletionService Test2 Many Thread Result : " + result);
}
}
标签:入门,int,Demo,containerNum,System,cpuNum,future,taskTime,多线程
From: https://blog.csdn.net/2401_86904339/article/details/141440617