致命异常:java.util.concurrent.RejectedExecutionException

时间:2016-10-05 08:13:47

标签: java android executorservice threadpoolexecutor

我正在尝试提交ExecutorService

时遇到下面提到的错误
  

致命异常:java.util.concurrent.RejectedExecutionException:从java.util.concurrent.ThreadPoolExecutor@427c1228拒绝任务java.util.concurrent.FutureTask@428a4d70 [关闭,池大小= 1,活动线程= 1,排队tasks = 0,completed tasks = 12]

这是我以前提交的代码

public class RunnablesQueue {
    private final int poolSize;
    private String name;
    private ExecutorService serialExecutor;

    public RunnablesQueue(String name) {
        this.name = name;
        this.poolSize = 1;
    }

    public RunnablesQueue(String name, int poolSize) {
        this.name = name;
        this.poolSize = poolSize;
    }

    /**
     * Offer a runnable to be queued for execution
     *
     * @param runnable Runnable to offer.
     */
    public void offer(final Runnable runnable) {
        serialExecutor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d("Running item from queue: " + name);
                    runnable.run();
                } catch (Exception e) {
                    Log.e(e, "Execution failed");
                }
            }
        });
    }

    /**
     * Start the queue.
     */
    public void start() {
        if (serialExecutor == null || serialExecutor.isShutdown()) {
            serialExecutor = Executors.newFixedThreadPool(poolSize);
        }
    }

    /**
     * Shutdown the queue.
     */
    public void shutdown() {
        if (serialExecutor != null && !serialExecutor.isShutdown()) {
            serialExecutor.shutdownNow();
        }
    }
 }

0 个答案:

没有答案