package com.zs.test;
import com.google.common.util.concurrent.*;
import com.zs.test.test.TestJavaThreadTest1;
import com.zs.test.test.ThreadListenerTest;
import java.util.concurrent.*;
/**
* @author zhaosen on 2018/12/7 9:36
* description TODO
**/
public class Test {
public static void main(String[] args) {
// futureTest();
for (int i = 0; i < 1000; i++) {
listenableFutureTest();
}
// completableFutureTest();
}
/**使用jdk1.8 方式
* @author zhaosen on 2018/12/11 16:55
* @param
* @return void
**/
public static void completableFutureTest() {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ExecutorService pool = new ThreadPoolExecutor(1,20,0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(30),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());
CompletableFuture<Long> executorService = CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}
System.out.println("run end ...");
return System.currentTimeMillis();
},pool);
long time = 0;
try {
time = executorService.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("time = "+time);
}
/**
* 使用guava 多线程
* @author zhaosen on 2018/12/11 17:53
* @param
* @return void
**/
public static void listenableFutureTest(){
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
ExecutorService pool = new ThreadPoolExecutor(10,13,0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(3),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());
try {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(pool);
// ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(49));
for (int i = 0; i < 10; i++) {
// TestJavaThreadTest1 threadTest1 = new TestJavaThreadTest1(i);
// ListenableFuture<Integer> listenableFuture = executorService.submit(threadTest1);
TestJavaThreadTest1 threadTest1 = new TestJavaThreadTest1(i);
final ListenableFuture<Integer> listenableFuture = executorService.submit(threadTest1);
ThreadListenerTest threadListenerTest = new ThreadListenerTest(listenableFuture);
//如果调用了此方法由于它需要启动线程来执行回调,所以需要的线程数要多于不使用此方法需要的数量
//根据实际测试需要设置的最大线程数的最小值 maximumPoolSize = (任务总数-最大队列排队数)* 2
listenableFuture.addListener(threadListenerTest, executorService);
}
((ThreadPoolExecutor) pool).getTaskCount();
System.out.println("for循环已结束");
} catch (Exception e) {
System.out.println("工作线程数:"+((ThreadPoolExecutor) pool).getTaskCount());
System.out.println("活跃线程数:"+((ThreadPoolExecutor) pool).getActiveCount());
System.out.println("完成线程数:"+((ThreadPoolExecutor) pool).getCompletedTaskCount());
System.out.println("核心线程数:"+((ThreadPoolExecutor) pool).getCorePoolSize());
//返回当前线程池中的线程数量
System.out.println("线程数:"+((ThreadPoolExecutor) pool).getPoolSize());
System.out.println("排队数:"+((ThreadPoolExecutor) pool).getQueue().size());
e.printStackTrace();
}
}
/**
* 常用的方法
* @author zhaosen on 2018/12/11 17:54
* @param
* @return void
**/
public static void futureTest(){
try {
// 固定大小的线程池 核心线程数和最大线程数=10
ExecutorService executorService = Executors.newFixedThreadPool(10);
// 记录开始时间
Long start = System.currentTimeMillis();
// 一个耗时的任务
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
@Override
public Boolean call() throws Exception {
//模拟耗时5s
TimeUnit.SECONDS.sleep(10);
return true;
}
});
// 阻塞 等待执行结果
Boolean result = future.get();
//打印结果
System.out.println("任务执行成功了,执行结果=" + result);
// 记录结束时间
Long end = System.currentTimeMillis();
// 执行时间
System.out.println("线程执行结束了,耗时=" + (end - start) + "毫秒");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
ThreadPoolExecutor源码分析 :https://blog.youkuaiyun.com/seasonLai/article/details/82624236
Guava包的ListenableFuture解析:https://www.kancloud.cn/wizardforcel/java-opensource-doc/112629
Future和CompletableFuture解析与使用:https://www.cnblogs.com/happyliu/archive/2018/08/12/9462703.html