在我的应用程序中我们使用了线程池,我们已经为线程池指定了超时但似乎没有调用超时,下面是代码:
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
调用。
如果线程在指定的超时时间内仍处于活动状态,我想要一种机制来停止线程。
答案 0 :(得分:1)
正如评论中已经指出的那样,您没有指定超时。您只指定了ThreadPoolExecutor的 keep-alive 时间。
keep-alive不会终止或中断正在运行的线程,而只会释放Executor的 idle 线程(请参阅getKeepAliveTime
)。