怎么建立一个购物网站,长沙网站建立公司,河北建设行业信息网站,网站建设什么是静态网页众所周知#xff0c;并行查询可以提高程序运行效率。主线程需要等待所有子线程把数据查询出结果#xff0c;如果没有设置超时时间#xff0c;就需要主线程就会一直阻塞到那里#xff0c;从而占用服务器资源#xff0c;那么如何设置超时时间呢? 1.在SpringBoot项目中引入线… 众所周知并行查询可以提高程序运行效率。主线程需要等待所有子线程把数据查询出结果如果没有设置超时时间就需要主线程就会一直阻塞到那里从而占用服务器资源那么如何设置超时时间呢? 1.在SpringBoot项目中引入线程池
EnableAsync
Configuration
public class ThreadPoolsConfig {Value(${AsyncTaskExecutor.corePooleSize:6})private Integer corePooleSize;Value(${AsyncTaskExecutor.maxPoolSize:15})private Integer maxPoolSize;Value(${AsyncTaskExecutor.queueCapacity:20000})private Integer queueCapacity;/*** 自定义线程池*/Bean(myTaskExecutor)public AsyncTaskExecutor getMyTaskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setThreadNamePrefix(TaskThreadExec--);executor.setCorePoolSize(corePooleSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);// 放弃等待队列中最旧的任务来添加新的任务executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());return executor;}}
2.使用java.util.concurrent.CompletableFuture进行并行查询未设置超时时间 CompletableFuture[] asyncList new CompletableFuture[]{CompletableFuture.runAsync(() - queryDataA(), asyncTaskExecutor),CompletableFuture.runAsync(() - queryDataB(), asyncTaskExecutor)};CompletableFuture.allOf(asyncList).join();
3.使用java.util.concurrent.CompletableFuture进行并行查询设置超时时间 CompletableFuture[] asyncList new CompletableFuture[]{CompletableFuture.runAsync(() - queryDataA(), asyncTaskExecutor),CompletableFuture.runAsync(() - queryDataB(), asyncTaskExecutor)};try {CompletableFuture.allOf(asyncList).get(3, TimeUnit.SECONDS);} catch (InterruptedException | ExecutionException | TimeoutException e) {System.err.println(多线程查询e.getMessage());Thread.currentThread().interrupt();}
需要说明的是这里的interrupt方法也可以不调用。
interrupt方法的作用如下: 线程A在执行sleep,wait,join时,线程B调用线程A的interrupt方法的确这一个时候A会有InterruptedException 异常抛出来。 但这其实是在sleep、wait、join这些方法内部会不断检查中断状态的值而自己抛出的InterruptedException import java.util.Date;public class MyThread extends Thread{Overridepublic void run() {while (!isInterrupted()){System.out.println(new Date());}}public static void main(String[] args) throws InterruptedException {MyThread myThread new MyThread();myThread.start();//1秒后打断子线程Thread.sleep(1000);myThread.interrupt();}
}