publicclassTaskimplementsRunnable{privatefinalString name;publicTask(String name){this.name = name;}@Overridepublicvoidrun(){System.out.println(Thread.currentThread().getName()+" → "+ name +" Start Time = "+newDate());processCommand();System.out.println(Thread.currentThread().getName()+" → "+ name +" End Time = "+newDate());}privatevoidprocessCommand(){try{Thread.sleep(3000);}catch(InterruptedException e){
e.printStackTrace();}}}
publicclassScheduledThreadPool{publicstaticvoidmain(String args[])throwsInterruptedException,ExecutionException{System.out.println(Thread.currentThread().getName()+"线程: Start at: "+newDate());ScheduledThreadPoolExecutor exec =(ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(5);for(int i =0; i <10; i++){System.out.println("添加了第"+ i +"个线程任务 "+newDate());
exec.schedule(newTask("线程名字"+ i),10,TimeUnit.SECONDS);// 延迟10秒执行}
exec.shutdown();System.out.println(Thread.currentThread().getName()+"线程: 打卡"+newDate());while(!exec.isTerminated()){// wait for all tasks to finish}System.out.println(Thread.currentThread().getName()+"线程: Finished all threads at:"+newDate());}}
执行结果
main线程: Start at: Sat May 20 12:51:32 CST 2023
添加了第0个线程任务 Sat May 20 12:51:32 CST 2023
添加了第1个线程任务 Sat May 20 12:51:32 CST 2023
添加了第2个线程任务 Sat May 20 12:51:32 CST 2023
添加了第3个线程任务 Sat May 20 12:51:32 CST 2023
添加了第4个线程任务 Sat May 20 12:51:32 CST 2023
添加了第5个线程任务 Sat May 20 12:51:32 CST 2023
添加了第6个线程任务 Sat May 20 12:51:32 CST 2023
添加了第7个线程任务 Sat May 20 12:51:32 CST 2023
添加了第8个线程任务 Sat May 20 12:51:32 CST 2023
添加了第9个线程任务 Sat May 20 12:51:32 CST 2023
main线程: 打卡Sat May 20 12:51:32 CST 2023
pool-1-thread-5 → 线程名字2 Start Time = Sat May 20 12:51:42 CST 2023
pool-1-thread-3 → 线程名字1 Start Time = Sat May 20 12:51:42 CST 2023
pool-1-thread-4 → 线程名字3 Start Time = Sat May 20 12:51:42 CST 2023
pool-1-thread-2 → 线程名字4 Start Time = Sat May 20 12:51:42 CST 2023
pool-1-thread-1 → 线程名字0 Start Time = Sat May 20 12:51:42 CST 2023
pool-1-thread-3 → 线程名字1 End Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-5 → 线程名字2 End Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-3 → 线程名字5 Start Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-5 → 线程名字6 Start Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-4 → 线程名字3 End Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-4 → 线程名字7 Start Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-2 → 线程名字4 End Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-2 → 线程名字8 Start Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-1 → 线程名字0 End Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-1 → 线程名字9 Start Time = Sat May 20 12:51:45 CST 2023
pool-1-thread-3 → 线程名字5 End Time = Sat May 20 12:51:48 CST 2023
pool-1-thread-5 → 线程名字6 End Time = Sat May 20 12:51:48 CST 2023
pool-1-thread-4 → 线程名字7 End Time = Sat May 20 12:51:48 CST 2023
pool-1-thread-2 → 线程名字8 End Time = Sat May 20 12:51:48 CST 2023
pool-1-thread-1 → 线程名字9 End Time = Sat May 20 12:51:48 CST 2023
main线程: Finished all threads at:Sat May 20 12:51:48 CST 2023
publicScheduledFuture<?>scheduleWithFixedDelay(Runnable command,// the task to executelong initialDelay,// the time to delay first executionlong delay,// the delay between the termination of one execution and the commencement of the nextTimeUnit unit);// the time unit of the initialDelay and delay parameters