当前位置 : 主页 > 编程语言 > java >

java限制方法执行时间;Future使用

来源:互联网 收集:自由互联 发布时间:2022-07-13
public static void main(String[] args) { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setCorePoolSize(3); threadPoolTaskExecutor.setMaxPoolSize(10); threadPoolTaskExecutor.setQueueCapa
public static void main(String[] args) {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(3);
threadPoolTaskExecutor.setMaxPoolSize(10);
threadPoolTaskExecutor.setQueueCapacity(200);
threadPoolTaskExecutor.setThreadNamePrefix("task-concurrent-work");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPoolTaskExecutor.initialize();
for (int i=0;i<15;i++){
Future<String> future= threadPoolTaskExecutor.submit(() -> {
System.out.println(123);
return null;
});
try{
future.get(1000,TimeUnit.MILLISECONDS);
}catch (Exception e){
System.out.println("失败");
}
}
}

 



上一篇:【Leetcode_easy】733. Flood Fill
下一篇:没有了
网友评论