gpt4 book ai didi

java - 相同的 Runnable 类被两次传递给同时运行两个/多个线程的 ExecutorService

转载 作者:行者123 更新时间:2023-11-30 04:37:22 24 4
gpt4 key购买 nike

我有多个可运行类,我想通过一个集中类(称为 Launcher 类,其中包含所有可运行类的列表)使用执行程序服务来运行它们。

我写了一个Launcher使用 applicationContext.getBean() 来实例化所有 bean 的类。每个可运行类还定义了 pool size对于它(为此可运行程序生成的线程数)。

public class DaemonsLauncher {

@Autowired
private ApplicationContext appContext;

private List<Daemon> daemons = new ArrayList<Daemon>();

private ScheduledThreadPoolExecutor executor;

private void initThreadPool() throws Exception {
//daemonNames coming at run time from somewhere.
List<String> daemonList = Arrays.asList(daemonNames.split(Constant.COMMA));
//Pool size will now be summation of all the pool sizes.
int poolSize = 0;
for (String dName : daemonList) {
Daemon daemon = appContext.getBean(dName, Daemon.class);
poolSize += daemon.getPoolSize();
daemons.add(daemon);
}
executor = new ScheduledThreadPoolExecutor(poolSize);
}

public void launchDaemons() throws Exception {
for (Daemon daemon : daemons) {
for (int currentThreadCount = 1; currentThreadCount <= daemon.getPoolSize(); currentThreadCount++) {
executor.scheduleWithFixedDelay(daemon, 0, XYZ, ABC);
}
}
}
}

在执行此操作的过程中,我将所有可运行项添加到 List<Daemon> (其中 Daemon 是一个抽象的可运行类,由其他守护进程扩展)。

public abstract class Daemon implements Runnable {

protected int poolSize = 1;

public int getPoolSize() {
return poolSize;
}

@Override
public void run() {
//Calls run methods of individual runnables here.
}

}

如您所见,我在执行时多次添加每个可运行类的相同实例(取决于池大小)。

executorScheduledThreadPoolExecutor .

此时,R的运行方法被调用时,我两次都找到了可运行类的相同实例,因为 appContext.getBean() 仅使用了一次它们(通过打印 hasCode 和 toString 进行检查)。

但是这些可运行类的多个实例是同时运行的。

这种情况可能存在什么问题?这些为同一个可运行对象运行的多个线程会导致问题还是会正常?

作为替代方案,根据 poolSize,我什至可以在此循环之前获取 Runnable 类的多个实例,并且仅循环一次,具体取决于列表中存在的所有“Runnable”条目。

  1. 所有 bean 本质上都是原型(prototype)(尽管它不会出现,因为我只调用了一次 appContext.getBean)。
  2. A sample Runnable class .

    public class R extends Daemon {
    @Override
    public void runIndividualDaemon() {
    //Called by run of Daemon class.
    logger.info("Running Daemon." + this.hashCode() + " " + this.toString());
    }

    @Override
    protected void init() throws Exception {
    this.poolSize = 5;
    }
    }

最佳答案

如果您的某个任务被执行了多次,那是因为您多次添加了该任务。

我建议您在添加任务时登录以确认这一点。

But how can it happen that same thread is performing task of two threads?

一个线程不能同时执行两项任务,但它可以重复使用同一个线程来执行一项又一项任务。

Is it that "I have two instances of 'Runnable' class" which would not have been the case if that bean was singleton?

我不认为在这种情况下添加两个相同的任务或添加相同的任务两次会产生很大的差异。

关于java - 相同的 Runnable 类被两次传递给同时运行两个/多个线程的 ExecutorService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13161625/

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