每次都写每次都查记录成册,以后就看这里。
- 创建线程池
- 线程池+runable
- 线程池+callable+异步获取
- 线程池+callable+同步获取
线程池+runable
创建线程池
package com.example.demo.threadPool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MyThreadPool {
public static ThreadPoolExecutor newInstance() {
return SingletonHolder.threadPoolExecutor;
}
/**
* 定义cpu核数+3数量的线程池
* 静态内部类方式使用时创建,线程安全
*/
public static class SingletonHolder{
private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors()/2;
private static final int MAX_POOL_SIZE = Runtime.getRuntime().availableProcessors();
private static final int QUEUE_CAPACITY = 1000;
private static final Long KEEP_ALIVE_TIME = 1L;
private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(QUEUE_CAPACITY),
new ThreadPoolExecutor.CallerRunsPolicy());
}
}
runable
实现
package com.example.demo.threadPool;
public class TestRunable implements Runnable {
@Override
public void run() {
System.out.println("子线程开始");
// 子线程休眠五秒
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程结束");
}
}
controller
调用`
@PostMapping("upload3")
public void upload3() throws Exception {
ThreadPoolExecutor pools = MyThreadPool.newInstance();
TestRunable testRunable = new TestRunable();
pools.submit(testRunable);
pools.submit(testRunable);
pools.submit(testRunable);
pools.submit(testRunable);
}
至此线程池+
runable
就完成了
线程池+callable
实现+异步获取
callable
实现
package com.example.demo.threadPool;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
public class OneTask implements Callable {
private int time;
private String name;
public OneTask(int time,String name) {
this.time = time ;
this.name = name;
}
@Override
public String call() throws Exception {
TimeUnit.SECONDS.sleep(this.time) ;
return name+"---success";
}
}
controller
’调用
@PostMapping("upload1")
public String upload1() throws Exception {
ThreadPoolExecutor pool =
new ThreadPoolExecutor(3, 3, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10)) ;
CompletionService<String> cs = new ExecutorCompletionService<>(pool) ;
cs.submit(new OneTask(3, "name" + 3)) ;
cs.submit(new OneTask(1, "name" + 1)) ;
cs.submit(new OneTask(2, "name" + 2)) ;
for (int i = 0; i < 3; i++) {
String s = cs.take().get();
log.info("获取返回结果---"+s);
}
pool.shutdown();
return "success";
}
可以看到打印出的顺序事1、2、3
刚好符合预期目标,3耗时最长第一个调用但最后返回,1最后调用耗时最短最先返回,CompletionService
对象的cs.take().get()
异步获取结果,三个任务按照各自执行的速度谁先完成先拿到谁的结果
[nio-8080-exec-2] c.e.d.controller.EasyExcelBIoController : 获取返回结果---name1---success
[nio-8080-exec-2] c.e.d.controller.EasyExcelBIoController : 获取返回结果---name2---success
[nio-8080-exec-2] c.e.d.controller.EasyExcelBIoController : 获取返回结果---name3---success
至此线程池+
callable
实现+异步获取完成
线程池+callable
+同步获取
callable实现同上
controller
调用
@PostMapping("upload2")
public String upload2() throws Exception {
ThreadPoolExecutor pool =
new ThreadPoolExecutor(3, 3, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10)) ;
Future f1 = pool.submit(new OneTask(3, "name" + 3));
Future f2 = pool.submit(new OneTask(1, "name" + 1));
Future f3 = pool.submit(new OneTask(2, "name" + 2));
List<Future> futures = Arrays.asList(f1, f2, f3);
for (Future future : futures) {
Object o = future.get();
log.info("获取返回结果---"+o);
}
pool.shutdown();
return "success";
}
可以看到打印出的顺序事3、1、2
刚好符合预期目标3耗时最长只有等3返回了1和2才返回,future.get()
同步获取结果。
[nio-8080-exec-5] c.e.d.controller.EasyExcelBIoController : 获取返回结果---name3---success
[nio-8080-exec-5] c.e.d.controller.EasyExcelBIoController : 获取返回结果---name1---success
[nio-8080-exec-5] c.e.d.controller.EasyExcelBIoController : 获取返回结果---name2---success
至此线程池+
callable
+同步获取完成