相关疑难解决方法(0)

在Java中处理InterruptedException

以下处理方式有什么区别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

283
推荐指数
6
解决办法
15万
查看次数

队列已满时ThreadPoolExecutor阻塞?

我试图使用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,因为任务数量超过了工作队列的大小.但是,我正在寻找的所需行为是让主线程阻塞,直到队列中有空间.完成此任务的最佳方法是什么?

java concurrency multithreading executorservice executor

53
推荐指数
4
解决办法
4万
查看次数

如何获得 RejectedExecutionException

任何人都可以为我提供一个获得RejectedExecutionException的示例 ,这可能是一个现实生活中的示例。提前致谢。

java multithreading pool exception

4
推荐指数
1
解决办法
2986
查看次数