Nee*_*raj 6 java concurrency multithreading
RejectedExecutionException使用ThreadPoolExecutorJava 时最好的处理方法是什么?
我想确保提交的任务不应该被忽视,并且肯定会被执行.截至目前,完成任务还没有艰难的实时要求.
我认为可以完成的事情之一是在循环中等待,直到我知道可运行队列中有空间,然后继续将其添加到队列中.
如果人们可以分享他们的经历会很高兴.
添加可能的解决方案我虽然:
while(executor.getQueue().remainingCapacity <= 0){
// keep looping
Thread.sleep(100);
};
//if the loop exits ,indicates that we have space in the queue hence
//go ahead and add to the queue
executor.execute(new ThreadInstance(params));
Run Code Online (Sandbox Code Playgroud)
我会改变你的队列的行为。例如
public class MyBlockingQueue<E> extends ArrayBlockingQueue<E> {
private final long timeoutMS;
public MyBlockingQueue(int capacity, long timeoutMS) {
super(capacity);
this.timeoutMS = timeoutMS;
}
@Override
public boolean offer(E e) {
try {
return super.offer(e, timeoutMS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将等待队列耗尽后再放弃。
| 归档时间: |
|
| 查看次数: |
3120 次 |
| 最近记录: |