以下处理方式有什么区别InterruptedException?最好的方法是什么?
try{
//...
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
Run Code Online (Sandbox Code Playgroud)
要么
try{
//...
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我也想知道这两个使用的场景.
java multithreading exception-handling interrupted-exception
我试图使用ThreadPoolExecutor执行许多任务.以下是一个假设的例子:
def workQueue = new ArrayBlockingQueue<Runnable>(3, false)
def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue)
for(int i = 0; i < 100000; i++)
threadPoolExecutor.execute(runnable)
Run Code Online (Sandbox Code Playgroud)
问题是我很快得到了java.util.concurrent.RejectedExecutionException,因为任务数量超过了工作队列的大小.但是,我正在寻找的所需行为是让主线程阻塞,直到队列中有空间.完成此任务的最佳方法是什么?
任何人都可以为我提供一个获得RejectedExecutionException的示例 ,这可能是一个现实生活中的示例。提前致谢。