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

时间:2018-05-01 08:34:27

标签: java multithreading threadpool

我希望有一个维持到恒定线程数的线程池。我希望所有因异常而死亡或正常终止的线程,产生精确的副本以保持线程数。

我已经尝试了以下代码,期望它会在循环中继续打印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);
    }
}

我按照answer ::

的建议将代码更改为以下内容
    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);
    }
}

输出不重复。我在这里做错了吗?

1 个答案:

答案 0 :(得分:3)

您无需重新启动死线程,当提交新任务时,执行程序将检查是否有必要创建新线程。

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

此外,如果您只是execute任务,它将只执行一次。如果您期望连续执行,schedule可以wait终止:

@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) {
    }
}
相关问题