java ThreadPoolExecutor 超时未兑现

java ThreadPoolExecutor time-out not honored

在我的应用程序中,我们使用了线程池,我们为线程池指定了一个超时,但似乎没有调用超时,下面是代码:

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ThreadPoolDemo extends Thread{

public void run(){
    System.out.println("Starting---" + new Timestamp((new Date()).getTime())  + "--" + Thread.currentThread().getName());
    try {
        Thread.sleep(30000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Finishing---" + new Timestamp((new Date()).getTime())  + "--" +Thread.currentThread().getName());
}

public static void main (String[] args){
    ArrayBlockingQueue<Runnable> threadQueue = new ArrayBlockingQueue<Runnable>(5);
    ThreadPoolExecutor thumbnailGeneratorThreadPool = new ThreadPoolExecutor(1, 3,
            5, TimeUnit.SECONDS, threadQueue);
    thumbnailGeneratorThreadPool.allowCoreThreadTimeOut(true);
    ArrayList fTasks = new ArrayList();
    for (int i = 0; i < 15; i++) {
        System.out.println("Submitting Thread : " + (i+1) + "- Current Queue size is : " + threadQueue.size());
        ThreadPoolDemo tpd = new ThreadPoolDemo();
        Future future = thumbnailGeneratorThreadPool.submit(tpd);
    }
    }
}

代码的输出是:

Submitting Thread : 1- Current Queue size is : 0
Submitting Thread : 2- Current Queue size is : 0
Submitting Thread : 3- Current Queue size is : 1
Submitting Thread : 4- Current Queue size is : 2
Submitting Thread : 5- Current Queue size is : 3
Submitting Thread : 6- Current Queue size is : 4
Submitting Thread : 7- Current Queue size is : 5
Submitting Thread : 8- Current Queue size is : 5
Submitting Thread : 9- Current Queue size is : 5
Exception in thread "main" java.util.concurrent.RejectedExecutionException
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:78)
    at ThreadPoolDemo.main(ThreadPoolDemo.java:33)
Starting---2016-10-26 14:20:16.254--pool-1-thread-2
Starting---2016-10-26 14:20:16.254--pool-1-thread-3
Starting---2016-10-26 14:20:16.254--pool-1-thread-1
Finishing---2016-10-26 14:20:46.261--pool-1-thread-1
Finishing---2016-10-26 14:20:46.261--pool-1-thread-2
Finishing---2016-10-26 14:20:46.261--pool-1-thread-3
Starting---2016-10-26 14:20:46.261--pool-1-thread-2
Starting---2016-10-26 14:20:46.261--pool-1-thread-3
Starting---2016-10-26 14:20:46.261--pool-1-thread-1
Finishing---2016-10-26 14:21:16.265--pool-1-thread-1
Starting---2016-10-26 14:21:16.265--pool-1-thread-1
Finishing---2016-10-26 14:21:16.265--pool-1-thread-3
Starting---2016-10-26 14:21:16.265--pool-1-thread-3
Finishing---2016-10-26 14:21:16.265--pool-1-thread-2
Finishing---2016-10-26 14:21:46.277--pool-1-thread-1
Finishing---2016-10-26 14:21:46.277--pool-1-thread-3

现在 ThreadPoolExecutor keepAliveTime 设置为 5 秒。

但是,如果我们看到输出,线程需要 30 秒才能完成。我不确定为什么 InterruptedException 没有被线程上的 ThreadPoolExecutor 调用。

如果线程在指定的超时后仍处于活动状态,我想要一种停止线程的机制。

如评论中所述,您没有指定超时。您只指定了 ThreadPoolExecutor 的 keep-alive 时间。 keep-alive 不会终止或中断 运行 线程,而只会释放 Executor 的 idle 线程(参见 getKeepAliveTime)。

如果你想为你的任务设置超时,你必须使用 invokeAll or invokeAny 方法而不是 submit。 另见

  • Killing thread after some specified time limit in Java
  • How to timeout a thread