一个线程池优化的例子。

假设有这样一个需求:查询商品的基本信息、查询商品的图片列表、查询商品的描述信息,最后再一块返回。

如果使用同步的方式,代码就是按照固定的顺序执行。

1
2
3
4
5
6
7
8
9
public Map<String, Object> detail(long goodsId) {
//创建一个map

//step1:查询商品基本信息,放入map
//step2:查询商品图片列表,返回一个集合放入map
//step3:查询商品描述信息,放入map

return map;
}

假设上面每个步骤耗时200ms,此方法总共耗时至少600毫秒。其他还涉及到网络传输耗时,估计总共会在700ms左右。但此接口是有优化空间的。

看一下上面的逻辑,整个过程是按顺序执行的,实际上3个查询之间是没有任何依赖关系,所以说3个查询可以同时执行,那对这3个步骤采用多线程并行执行。

比如下面的方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

public class DemoApplication {

public static void main(String[] args) throws ExecutionException, InterruptedException {
long starTime = System.currentTimeMillis();
Map<String, String> map = new DemoApplication().funcabc();
System.out.println(map);
System.out.println("耗时(ms):" + (System.currentTimeMillis() - starTime));
}

//创建个线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);

public String funcA() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(200);
return "执行方法funcA";
}

public String funcB() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(200);
return "执行方法funcB";
}

public String funcC() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(200);
return "执行方法funcC";
}

public Map<String, String> funcabc() throws ExecutionException, InterruptedException {
HashMap<String, String> result = new HashMap<>();

Future<String> resultA = executorService.submit(this::funcA);
Future<String> resultB = executorService.submit(this::funcB);
Future<String> resultC = executorService.submit(this::funcC);

result.put("funca", resultA.get());
result.put("funcb", resultB.get());
result.put("funcc", resultC.get());

return result;
}
}

上面的代码使用了异步和线程池的方式,最后通过Future获取异步执行结果。

最后执行的结果为:

1
2
{funca=执行方法funcA, funcb=执行方法funcB, funcc=执行方法funcC}
耗时(ms):227