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