使用多个线程排序时出错

时间:2016-03-04 07:00:53

标签: java

我在这个错误上搜索了很多stackoverflow问题。很多都与android有关。我无法使用这些答案来解决这个问题。我收到以下错误

Exception in thread "pool-1-thread-2" Exception in thread "pool-1-thread-4" java.util.concurrent.RejectedExecutionException: Task github.MultiThreadSort$1@e70657d rejected from java.util.concurrent.ThreadPoolExecutor@bf3f1b0[Shutting down, pool size = 5, active threads = 2, queued tasks = 0, completed tasks = 6262543]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at github.MultiThreadSort.quickSort0(MultiThreadSort.java:117)
    at github.MultiThreadSort.access$0(MultiThreadSort.java:74)
    at github.MultiThreadSort$1.run(MultiThreadSort.java:119)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.RejectedExecutionException: Task github.MultiThreadSort$1@fcc7491 rejected from java.util.concurrent.ThreadPoolExecutor@bf3f1b0[Shutting down, pool size = 5, active threads = 2, queued tasks = 0, completed tasks = 6262543]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at github.MultiThreadSort.quickSort0(MultiThreadSort.java:117)
    at github.MultiThreadSort.access$0(MultiThreadSort.java:74)
    at github.MultiThreadSort$1.run(MultiThreadSort.java:119)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是相关代码:

private static final int POOL_SIZE = Runtime.getRuntime().availableProcessors() + 1;
private static String inputFile = "/tmp/word-sort-master/src/sowpods.txt";
private static final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
private static ThreadPoolExecutor executor = new ThreadPoolExecutor(POOL_SIZE, POOL_SIZE, 0, TimeUnit.SECONDS,queue);

private void quickSort0(final int l, final int r) {
        int i = 0, j = 0;
        String temp = null;
        if (l >= r) {
            mainLock.lock();
            try {
                count--;
                if (count == 0) {
                    overCondition.signal();
                }
            } finally {
                mainLock.unlock();
            }
            return;
        }
        i = l;
        j = r;
        temp = data.get(i);
        while (i != j) {
            while (j > i && (data.get(j).compareTo(temp) >= 0)) {
                j--;
            }
            if (i < j) {
                data.set(i++, data.get(j));
            }
            while (j > i && data.get(i).compareTo(temp) <= 0) {
                i++;
            }
            if (i < j) {
                data.set(j--, data.get(i));
            }
        }
        data.set(i, temp);
        mainLock.lock();
        try {
            count--;
            if (count == 0) {
                overCondition.signal();
            }
        } finally {
            mainLock.unlock();
        }
        final int s = i;
        executor.execute(new Runnable() {
            public void run() {
                quickSort0(l, s - 1);
            }
        });
        executor.execute(new Runnable() {
            public void run() {
                quickSort0(s + 1, r);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

根据抛出的异常,新任务被拒绝,因为执行程序当前处于(或已经)关闭状态。

代码似乎没有等到排队的任务完成,例如通过等待ExecutorService#submit返回的Future。虽然似乎使用了某种类型的Condition信令(显示的代码不够......),但机制并不清楚,可能有问题。