博主首页

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

    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("失败");
           }
        }
    }

 

posted @ 2022-07-06 17:10  笑~笑  阅读(402)  评论(0编辑  收藏  举报