基本上,我希望能够运行多个线程——这些线程将在给定的时间段内使用睡眠命令。我希望能够在线程已经运行一段时间后根据用户输入来操纵这些睡眠线程的持续时间。
例如:
从classA开始线程...
private final ExecutorService scheduler = Executors.newCachedThreadPool();
public void startJob(Job job, List <Object> objectList) {
//Store the results of this in a map using a future and the id of job??
scheduler.submit(jobThreadInterface.create(job, objectList));
}
JobThreadInterface 启动 classB...
public class ClassB implements Runnable{
private Job job;
private List <Object> objectList;
private int changeSleepDuration;
public ClassB (Job job, List <Object> objectList){
this.job = job;
this.objectList= objectList;
}
public void run() {
//It will keep looping through this sleep command until there are no more objects left...
for (Object object : objectList){
if (object.getSleepNumber() > 0){
Thread.sleep(object.getSleepNumber() + changeSleepDuration);
}
}
public setChangeSleepDuration(int i){
changeSleepDuration = i;
}
}
}
所以基本上,我想要做的是setChangeSleepDuration
从 classA 访问我想要访问的任何线程的 ClassB 中的方法。这可能吗?如果可以,最好的方法是什么?
谢谢,