1 package com.example.demo.thread.threadpool; 2 3 import java.util.Random; 4 import java.util.concurrent.*; 5 6 /** 7 * 文件名:ScheduledThreadPool 8 * 作 者:Miles zhu 9 * 时 间:2019/7/30 9:49 10 * ------------------------- 11 * 功能和描述: 12 **/ 13 public class TestScheduledThreadPool { 14 public static void main(String[] args) throws ExecutionException, InterruptedException { 15 ScheduledExecutorService pool = Executors.newScheduledThreadPool(5); 16 for (int i = 0; i < 5; i++) { 17 Future<Integer> future = pool.schedule(new Callable<Integer>() { 18 @Override 19 public Integer call() throws Exception { 20 int num = new Random().nextInt(100); 21 System.out.println(Thread.currentThread().getName() + "-" + num); 22 return num; 23 } 24 }, 3, TimeUnit.SECONDS); 25 System.out.println(future.get()); 26 } 27 pool.shutdown(); 28 } 29 }