一 摘要
当使用java线程池的时候,如果需要获取每个线程返回值,有两种方式:
- 设置一个全局变量,每个线程将结果写入这个变量中
- 每个线程的返回future,通过future.get获取结果
下面针对这两个方法分别设计测试函数,对比两种方法的效果
二 全局变量方式
全局变量有三种方式:
- ArrayList:将结果写入ArrayList中,但是由于线程安全问题,需要手动加锁
- Vector:和第一种方法相比,不用手动加锁,因为Vector是线程安全的,实际上效果和手动加锁差不多。
- ConcurrentHashMap:由于ConcurrentHashMap是给桶加锁的,默认是16个桶,所以桶的数量越多,效果会比1,2两种方法好,因为在多线程的情况下,会有更多的线程同时获取锁,并且写入数据。最终的结果,可以直接使用map的values即可。
下面是测试代码:
public class ThreadConcurrentHashMap implements Runnable{
public static ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
String name;
public ThreadConcurrentHashMap(String name) {
this.name = name;
}
@Override
public void run() {
long begin = System.currentTimeMillis();
int index = (int)(Math.random() * 99999999);
int result = 0;
//随便写的
for (int i = 1; i < index; i++) {
result = (result / i ) * index;
}
long end = System.currentTimeMillis();
concurrentHashMap.put(name, "线程" + name + "执行结束花费:" + (end - begin) + "毫秒。");
}
public static void main(String[] args) {
ExecutorService executors = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
char name = (char) (97 + i);
executors.submit(new ThreadConcurrentHashMap(String.valueOf(name)));
}
executors.shutdown();
try {
long begin = System.currentTimeMillis();
executors.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
long end = System.currentTimeMillis();
System.out.println("总共花费时间:" + (end - begin) + "毫秒。");
} catch (InterruptedException e) {
e.printStackTrace();
}
//打印所有的值
for (String s : concurrentHashMap.values()) {
System.out.println(s);
}
}
}
结果:
总共花费时间:666毫秒。
线程a执行结束花费:666毫秒。
线程b执行结束花费:315毫秒。
线程c执行结束花费:129毫秒。
线程d执行结束花费:365毫秒。
线程e执行结束花费:307毫秒。
三 Future 方式获取
方法是将每个线程的返回的future 保存在list中,等待所有的线程结束,再调用get方法获取线程的返回值。如果直接调用get方法,主线程会阻塞到该线程结束才继续执行。
测试代码如下:
public class ThreadWithCallback implements Callable{
private String name;
public ThreadWithCallback(String name) {
this.name = name;
}
@Override
public Object call() throws Exception {
long begin = System.currentTimeMillis();
int index = (int)(Math.random() * 99999999);
int result = 0;
//随便写的
for (int i = 1; i < index; i++) {
result = (result / i ) * index;
}
long end = System.currentTimeMillis();
return ("线程" + name + ",花费:" + (end - begin) + "毫秒。");
}
public static void main(String[] args) {
ExecutorService executors = Executors.newCachedThreadPool();
try {
/* 启动线程时会返回一个Future对象。
* 可以通过future对象获取现成的返回值。
* 在执行future.get()时,主线程会堵塞,直至当前future线程返回结果。
*/
List<Future> futureList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
char name = (char) (97 + i);
Future future = executors.submit(new ThreadWithCallback(String.valueOf(name)));
futureList.add(future);
}
executors.shutdown();
long begin = System.currentTimeMillis();
executors.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
long end = System.currentTimeMillis();
System.out.println("总共花费时间:" + (end - begin) + "毫秒。");
//打印所有的值
for (Future future : futureList) {
System.out.println(future.get());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
总共花费时间:828毫秒。
线程a,花费:828毫秒。
线程b,花费:607毫秒。
线程c,花费:712毫秒。
线程d,花费:750毫秒。
线程e,花费:149毫秒。
四 总结
两种方式,可以根据个人喜好选择使用哪种方式,测试了几种感觉,感觉使用concurrenthashmap总花费时间较少。