gpt4 book ai didi

java - 使用 ExecutorService 执行任务 : Can I submit task within another task?

转载 作者:行者123 更新时间:2023-11-30 09:09:00 26 4
gpt4 key购买 nike

我想在现有任务中提交一个新任务以防失败(异常),但在运行以下代码时遇到了一些问题:

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 是一个更好的选择。谢谢大家!

最佳答案

那个异常is thrown when a task is submitted and either :

  • 执行器已关闭
  • 您超过了最大队列大小

如果不看代码我们无法分辨,但很可能你在隐藏代码中做了这两件事之一(甚至可能在 main 中),你必须修复它(顺便说一句,这种行为可以用 setRejectedExecutionHandler 定制)

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

关于java - 使用 ExecutorService 执行任务 : Can I submit task within another task?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com