2016-02-11 13 views
5

私はThreadPoolExecutorを持っており、タスクを提出しています。将来のタスクはThreadPoolExecutorから拒否されました

private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); 

このコードはThreadPoolExecutorRunnableを提出します。

protected void waitAndSweep(final String symbol) { 

    runnable = new Runnable() { 
     public void run() { /* irrelevant code */ } 
    }; 

    try { 
     Future<?> self = threadPoolExecutor.submit(runnable); 
     futures.add(self); 
    } catch (RejectedExecutionException re) { 
     /* this exception will be thrown when wait and sweep is called more than twice. 
     * threadPoolExecutor can have one running task and one waiting task. 
     */ 
    } catch (Exception e) { 
     logEvent(StrategyEntry.ERROR, "waitAndSweep", symbol, "Exception caught...", e); 
    } 
    } 

次のコードは、タスクを停止します。

ここ
protected synchronized void stop(StrategyEntry entry) throws Exception { 
    for (Object future : futures) { 
     ((Future<?>) future).cancel(true); 
    } 
    futures.clear(); 

    threadPoolExecutor.shutdown(); 
} 

問題がある:

タスクjava.util.concurrent.FutureT[email protected]はjava.util.concurrentのから拒否:私はタスクを停止しようとすると、私は次の例外を取得しています。 ThreadPoolExecutor @ 216393fb [終端、プールサイズ= 0、アクティブなスレッド= 0、キューに入れられたタスク= 0、完了したタスクは= 1]

+0

このエグゼキュータではどこでも 'shutdown()'を呼び出していますか? – RAnders00

+0

停止方法ではyes – MMPgm

+0

容量1のバッキングキューを作成していますが、すでに他のタスクがありますか? –

答えて

5

の問題は、あなたshutdown() stopメソッドでexcutor。タスクが完了するのを待つだけの場合は、Future.get()を使用します。エグゼキュータがシャットダウンされると、タスクはもう実行できなくなります。

shutdown()は、実際にアプリケーションを終了したい場合にのみ使用してください。

+0

エグゼキュータがシャットダウンされているときに、新しいタスクを投稿したくありません – MMPgm

+0

@MMPgmあなたの質問から、 'stop() 'メソッドを使用して実際に行います。多分あなたはそれを明確にすることができますか? – RAnders00

+0

Stopメソッドは待機中のタスクをすべて終了し、他のタスクを受け入れるべきではありません。例外が発生する可能性のあるケースが何であるかを知りたかっただけです。 – MMPgm

関連する問題