在我的应用程序中,我们已经使用了线程池,我们已经为线程池指定了超时,但似乎超时没有被调用,下面是代码:
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的空闲线程(参见getKeepAliveTime
)。
如果你想为你的任务设置一个超时,你必须使用invokeAll
或invokeAny
方法而不是submit
。参见
- 在Java和 中在指定时间限制后终止线程
- 如何使线程超时