@FunctionalInterface publicinterfaceCallable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ Vcall()throws Exception; }
/** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the {@code Executor} implementation. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be * accepted for execution * @throws NullPointerException if command is null */ voidexecute(Runnable command); }
finalSimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss"); while (!threadpool.awaitTermination(1L, TimeUnit.SECONDS)) { System.out.println("线程池中还有任务在执行,当前时间:" + sdf.format(newDate())); }
System.out.println("线程池中已经没有在执行的任务,线程池已完全关闭!"); }
控制台输出的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
complete a short task... 线程池是否已经关闭:true complete a short task... complete a short task... 线程池中还有任务在执行,当前时间:19:53:08 线程池中还有任务在执行,当前时间:19:53:09 线程池中还有任务在执行,当前时间:19:53:10 线程池中还有任务在执行,当前时间:19:53:11 线程池中还有任务在执行,当前时间:19:53:12 线程池中还有任务在执行,当前时间:19:53:13 线程池中还有任务在执行,当前时间:19:53:14 线程池中还有任务在执行,当前时间:19:53:15 线程池中还有任务在执行,当前时间:19:53:16 complete a long task 线程池中已经没有在执行的任务,线程池已完全关闭!
java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.lzumetal.multithread.threadpooltest.LongTask.run(LongTask.java:11) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 还没有执行的任务数:2 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 线程池是否已经关闭:true at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) complete a long task at java.lang.Thread.run(Thread.java:748) complete a long task java.lang.InterruptedException: sleep interrupted complete a long task at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.lzumetal.multithread.threadpooltest.LongTask.run(LongTask.java:11) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at com.lzumetal.multithread.threadpooltest.LongTask.run(LongTask.java:11) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 线程池中已经没有在执行的任务,线程池已完全关闭!