使用场景
本地有一个List,需要把list中的每一条数据传第三方公司的A、B、C、D、E、F共六个接口。那么如果一个数据传给A、再传给B、…最后传给E,那么会耗费很长时间。如果开6个线程,每个线程只访问A-E中的一个接口,那么时间就会缩短6倍。
实现方式以及使用
- 定义一个线程池
import java.util.concurrent.*;
public class CommonThreadPool {
private static ExecutorService exec = new ThreadPoolExecutor(50, 100, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10000),
new ThreadPoolExecutor.CallerRunsPolicy());
public static void execute(Runnable command) {
exec.execute(command);
}
/**
* 子线程执行结束future.get()返回null,若没有执行完毕,主线程将会阻塞等待
* @param command
* @return
*/