java编程中,经常会利用Executors的newXXXThreadsPool生成各种线程池,今天写了一小段代码,简单测试了下三种常用的线程池:

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger; /**
* 测试类(因为要用到forkjoin框架,所以得继承自RecursiveXXX)
*/
public class MathTest extends RecursiveAction { private List<Integer> target; private static AtomicInteger count = new AtomicInteger(0); public MathTest(List<Integer> list) {
this.target = list;
} public double process(Integer d) {
//模拟处理数据耗时200ms
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println("thread:" + Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + ", d: " + d);
return d;
} @Override
protected void compute() {
if (target.size() <= 2) {
for (Integer d : target) {
process(d);
count.incrementAndGet();
}
return;
}
int mid = target.size() / 2;
MathTest t1 = new MathTest(target.subList(0, mid));
MathTest t2 = new MathTest(target.subList(mid, target.size()));
t1.fork();
t2.fork();
} public static void main(String[] args) {
int num = 100;
int threadCount = 4;
List<Integer> target = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
target.add(i);
} MathTest test = new MathTest(target); //原始方法,单线程跑
long start = System.currentTimeMillis();
for (int i = 0; i < target.size(); i++) {
test.process(target.get(i));
}
long end = System.currentTimeMillis();
System.out.println("原始方法耗时:" + (end - start) + "\n"); //固定线程池
final ThreadFactory fixedFactory = new ThreadFactoryBuilder().setNameFormat("fixed-%d").build();
ExecutorService service = Executors.newFixedThreadPool(threadCount, fixedFactory); count.set(0);
start = System.currentTimeMillis();
for (Integer d : target) {
service.submit(() -> {
test.process(d);
count.incrementAndGet();
});
}
while (true) {
if (count.get() >= target.size()) {
end = System.currentTimeMillis();
System.out.println("fixedThreadPool耗时:" + (end - start) + "\n");
break;
}
} //cached线程池
final ThreadFactory cachedFactory = new ThreadFactoryBuilder().setNameFormat("cached-%d").build();
service = Executors.newCachedThreadPool(cachedFactory);
count.set(0);
start = System.currentTimeMillis();
for (Integer d : target) {
service.submit(() -> {
test.process(d);
count.incrementAndGet();
});
}
while (true) {
if (count.get() >= target.size()) {
end = System.currentTimeMillis();
System.out.println("cachedThreadPool耗时:" + (end - start) + "\n");
break;
}
} //newWorkStealing线程池
service = Executors.newWorkStealingPool(threadCount);
count.set(0);
start = System.currentTimeMillis();
for (Integer d : target) {
service.submit(() -> {
test.process(d);
count.incrementAndGet();
});
}
while (true) {
if (count.get() >= target.size()) {
end = System.currentTimeMillis();
System.out.println("workStealingPool耗时:" + (end - start) + "\n");
break;
}
} //forkJoinPool
ForkJoinPool forkJoinPool = new ForkJoinPool(threadCount);
count.set(0);
start = System.currentTimeMillis();
forkJoinPool.submit(test);
while (true) {
if (count.get() >= target.size()) {
end = System.currentTimeMillis();
System.out.println("forkJoinPool耗时:" + (end - start) + "\n");
break;
}
} }
}

代码很简单,就是给一个List,然后对里面的每个元素做处理(process方法),用三种线程池分别跑了一下,最后看耗时,输出如下:

原始方法耗时:20156

fixedThreadPool耗时:5145

cachedThreadPool耗时:228

workStealingPool耗时:5047

forkJoinPool耗时:5042

环境:mac + intel i5(虚拟4核)。 workStealingPool内部其实就是ForkJoin框架,所以二者在耗时上基本一样,符合预期;如果业务的处理时间较短,从测试结果来看,cachedThreadPool最快。

最新文章

  1. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
  2. 【转】php Thread Safe(线程安全)和None Thread Safe(NTS,非 线程安全)之分
  3. [转] --- Error: “A field or property with the name was not found on the selected data source” get only on server
  4. 2016HUAS暑假集训训练题 G - Oil Deposits
  5. OpenShift
  6. Centos7 安装 Nginx
  7. 生成json对象
  8. 继电器Relay:ZZR08
  9. 盒模型Box Model(浮动)
  10. 追踪CM_CONTROLCHANGE消息的产生和执行过程,可以较好的领会VCL的思想(就是到处通知,但耦合性很弱)
  11. WordPress插件开发记录
  12. [原创.数据可视化系列之十三]idw反距离权重插值算法的javascript代码实现
  13. Android开源项目——带图标文字的底部导航栏IconTabPageIndicator
  14. 百度TTS的来由
  15. 微信小程序测试
  16. shell实战之日志备份
  17. Flask实战-留言板-安装虚拟环境、使用包组织代码
  18. 【转】每天一个linux命令(1):ls命令
  19. AndroidStudio2.2.2 打开ddms快捷键
  20. DHCP配置实例

热门文章

  1. 打造好用的编辑终端环境yakuake
  2. 例说linux内核与应用数据通信系列【转】
  3. shell文本过滤编程(一):grep和正则表达式【转】
  4. Android启动过程深入解析【转】
  5. CRB and Candies LCM 性质
  6. Axure基础操作
  7. JMeter特殊情况二:针对某些请求数据每次请求都是变化的情况
  8. eclipse启动Tomcat时报错:严重: Exception loading sessions from persistent storage
  9. Java Servlet Filter
  10. 安卓Webview缓存网页数据(无网络正常显示)