如何在 Java 中使用 ScheduledThreadpoolExecutor 重新启动终止的线程?

How to restart terminated threads using ScheduledThreadpoolExecutor in Java?

我想要一个线程池,该线程池的线程数保持不变。我希望所有因异常而死亡或正常终止的线程都生成它们的精确副本以维持线程数。

我已经尝试了以下代码,期望它会在循环中不断打印 1-2-3-4-1-2-3-4.... 但是它在第一次执行后停止.

import org.junit.Test;
import java.util.Optional;
import java.util.concurrent.*;

public class Testing {
    @Test
    public void tet() {
        ScheduledExecutorService poolExecutor =
                new CustomExecutor(4);
        for (int i = 1; i <= 4; i++) {
            poolExecutor.execute(new Task(i));
        }
    }

}

class Task implements Runnable {

    int x;

    Task(int x) {
        this.x = x;
    }

    @Override
    public void run() {
        System.out.println(x);
        if (true) {
            System.out.println("throwing exception " + x);
            throw new RuntimeException();
        }
    }
}

class CustomExecutor extends ScheduledThreadPoolExecutor {

    public CustomExecutor(int corePoolSize) {
        super(corePoolSize);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        this.execute(r);
    }
}

我按照答案中的建议将代码更改为以下内容::

    public class Testing {
    @Test
    public void tet() {
        ScheduledExecutorService poolExecutor = new ScheduledThreadPoolExecutor(4);
        for (int i = 1; i <= 4; i++) {
            poolExecutor.scheduleAtFixedRate(new Task(i), 0, 1000, TimeUnit.MILLISECONDS);
        }
    }
}
class Task extends Thread {

    int x;

    Task(int x) {
        this.x = x;
    }

    @Override
    public void run() {
        System.out.println(x);
    }
}

输出不重复。我是不是做错了什么?

不需要手动重启死线程,当有新任务提交时,执行器会检查是否需要创建新线程。

在这种情况下,如果您希望任务在异常发生后保持运行,您可以捕获并处理它。

另外,如果你只是execute这个任务,它只会被执行一次。如果您希望连续执行以及 wait 终止,您应该 schedule 它们:

@Test
public void tet() {
    ScheduledExecutorService poolExecutor =
            new CustomExecutor(4);
    for (int i = 1; i <= 4; i++) {
        poolExecutor.scheduleAtFixedRate(new Task(i), 0, 1000, TimeUnit.MILLISECONDS);
    }
    try {
        poolExecutor.awaitTermination(10000, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
    }
}