使用ExecutorService执行任务:我可以在其他任务中提交任务吗?

时间:2014-04-23 17:25:44

标签: java multithreading threadpool executorservice

我想在发生故障的情况下在现有任务中提交新任务(例外),但在运行以下代码时出现了一些问题:

public class TestTask implements Runnable{
private int myInt;
private ExecutorService exec;

public TestTask(int x, ExecutorService s){
    this.myInt=x;
    this.exec=s;
}

@Override
public void run() {         
    try{
        //print number if it's odd; otherwise throw exception
        if(this.myInt%2 ==0) throw new Exception();     
        else System.out.println("Asynchronous task: "+ this.myInt); //do sth

    } catch (Exception e) {
        System.out.println("resubmitting..");
        //## TODO: PROBLEM??
        this.exec.execute(new TestTask(this.myInt+1, this.exec));
    }
}

public static void main(String[] args) {
    int NTHREADS =2;
    final ExecutorService service= Executors.newFixedThreadPool(NTHREADS);
    //run tasks
    for(int i=0; i<10; i++){
        service.execute(new TestTask(i, service) );
    }
    //...
}
//....

代码编译并运行几步然后抛出 java.util.concurrent.RejectedExecutionException 。任何想法如何解释这个以及如何解决?谢谢!

[编辑:下面添加了堆栈跟踪]


resubmitting..
Asynchronous task: 1
resubmitting..
Asynchronous task: 3
resubmitting..
Asynchronous task: 5
resubmitting..
Exception in thread "pool-1-thread-2" java.util.concurrent.RejectedExecutionException: Task pe.ads.TestTask@4e19b97c rejected from java.util.concurrent.ThreadPoolExecutor@322ba3e4[Shutting down, pool size = 2, active threads = 2, queued tasks = 8, completed tasks = 3]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
    at pe.ads.TestTask.run(TestTask.java:26)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Exception in thread "pool-1-thread-1" java.util.concurrent.RejectedExecutionException: Task pe.ads.TestTask@31eb494e rejected from java.util.concurrent.ThreadPoolExecutor@322ba3e4[Shutting down, pool size = 2, active threads = 2, queued tasks = 8, completed tasks = 3]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
    at pe.ads.TestTask.run(TestTask.java:26)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Asynchronous task: 7Exception in thread "pool-1-thread-3" java.util.concurrent.RejectedExecutionException: Task pe.ads.TestTask@35afe17b rejected from java.util.concurrent.ThreadPoolExecutor@322ba3e4[Shutting down, pool size = 2, active threads = 2, queued tasks = 6, completed tasks = 6]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
    at pe.ads.TestTask.run(TestTask.java:26)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

resubmitting..
Asynchronous task: 9
Asynchronous task: 1
Exception in thread "pool-1-thread-5" java.util.concurrent.RejectedExecutionException: Task pe.ads.TestTask@53e64a3b rejected from java.util.concurrent.ThreadPoolExecutor@322ba3e4[Shutting down, pool size = 2, active threads = 2, queued tasks = 3, completed tasks = 8]
resubmitting..
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
    at pe.ads.TestTask.run(TestTask.java:26)
Asynchronous task: 1    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

[编辑:已解决]

事实证明我在for循环之后立即调用了shutdown(),

    for(int i=0; i<10; i++){
        service.execute(new TestTask(i, service) );
    }
    service.shutdown();

所以新提交的任务被拒绝了。还了解到ThreadPoolExecutor是一个更好的选择。谢谢你们!

2 个答案:

答案 0 :(得分:1)

您可以继承ThreadPoolExecutor并在那里包含重新提交逻辑。它会更优雅。 ThreadPoolExecutor设计精良,提供了钩子方法来满足您的需求。您可能希望覆盖ThreadPoolExecutor.afterExecute

答案 1 :(得分:1)

异常is thrown when a task is submitted and either

  • 执行人已被关闭
  • 您超出了最大队列大小

我们无法看到代码,但你可能会在隐藏代码中做这两件事之一(甚至可能在main中),你必须修复它(顺便说一下,这种行为可以定制与setRejectedExecutionHandler

我不知道这个问题是你的实际问题还是样本问题,但无论如何我不认为任务产生另一个任务是常见的做法。您最好重新构建代码,以便调用者自己在必要时提交新任务。并不是说它会解决你的问题,但肯定会改进你的程序设计。如果您描述自己的域名,也许我们也可以为您提供帮助。