ThreadPoolExecutor的使用 (获取子线程执行结果后再执行主线程) 和CountDownLanch的简要使用
一、ThreadPoolExecutor 简要实例
获取子线程执行结果后再执行主线程 ,这样可将复杂耗时业务拆分执行返回结果,将结果汇总整理。
多个线程时可以 利用Future阻塞,当其它线程执行完毕获得结果,再执行主线程
/**
* ThreadPoolExecutor 线程池使用
* 1、多个线程时可以 利用Future阻塞,当其它线程执行完毕获得结果,再执行主线程
*/
@Test
public void simpleMethod() {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(9, 100, 100, TimeUnit.SECONDS, new SynchronousQueue<>());
try {
List list1 = new ArrayList();
Callable call1 = new Callable() {
@Override
public Integer call() throws Exception {
for (int j = 0; j < 10; j++) {
list1.add("i:" + j);
}
return 10;
}
};
Callable call2 = new Callable() {
@Override
public Integer call() throws Exception {
for (int j = 0; j < 10; j++) {
list1.add("i:" + j);
}
return 10;
}
};
Callable call3 = new Callable() {
@Override
public Integer call() throws Exception {
for (int j = 0; j < 10; j++) {
list1.add("i:" + j);
}
return 10;
}
};
Callable call4 = new Callable() {
@Override
public Integer call() throws Exception {
for (int j = 0; j < 10; j++) {
list1.add("i:" + j);
}
return 10;
}
};
Future f1 = poolExecutor.submit(call1);
Future f2 = poolExecutor.submit(call2);
Future f3 = poolExecutor.submit(call3);
Future f4 = poolExecutor.submit(call3);
ArrayList ss = new ArrayList<>();
f1.get();
f2.get();
f3.get();
f4.get();
// Thread.sleep(1000);
System.out.println("list1.size() = " + list1.size());
} catch (Exception e) {
e.printStackTrace();
}
}
二、CountDownLanch 简要实例
场景:当指定几个线程执行完后再执行 主线程,或使用2个CountDownLanch 对象进行多组子线程控制
/**
* CountDownLatch 阻塞执行完预其的线程才会执行,后续方法业务处理
*/
@Test
public void lanchTest() {
try {
ThreadPoolExecutor thredPool = new ThreadPoolExecutor(20, 100, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
CountDownLatch cutdownLanch = new CountDownLatch(3);
thredPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("我是子线程-id:" + Thread.currentThread().getName());
cutdownLanch.countDown();
}
});
thredPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("我是子线程-id:" + Thread.currentThread().getName());
cutdownLanch.countDown();
}
});
thredPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("我是子线程-id:" + Thread.currentThread().getName());
cutdownLanch.countDown();
}
});
System.out.println("这是主线程在下一句阻塞,current-thread-id:" + Thread.currentThread().getName());
cutdownLanch.await();
System.out.println("子线程执行完毕");
} catch (InterruptedException e) {
e.printStackTrace();
}
}