小编Rav*_*Rao的帖子

Java ThreadPoolExecutor在使用ArrayBlockingQueue时卡住了

我正在开发一些应用程序并使用ThreadPoolExecutor来处理各种任务.一段时间后,ThreadPoolExecutor陷入困境.为了在更简单的环境中模拟这个,我编写了一个简单的代码,我可以模拟这个问题.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolExecutor {
    private int poolSize = 10;
    private int maxPoolSize = 50;
    private long keepAliveTime = 10;
    private ThreadPoolExecutor threadPool = null;
    private final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
            100000);

    public MyThreadPoolExecutor() {
        threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize,
                keepAliveTime, TimeUnit.SECONDS, queue);
        threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {

            @Override
            public void rejectedExecution(Runnable runnable,
                    ThreadPoolExecutor threadPoolExecutor) {
                System.out
                        .println("Execution rejected. Please try restarting the application.");
            }

        });
    }

    public void runTask(Runnable task) {
        threadPool.execute(task); …
Run Code Online (Sandbox Code Playgroud)

java queue multithreading deadlock blocked-threads

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

blocked-threads ×1

deadlock ×1

java ×1

multithreading ×1

queue ×1