基本上,我希望能夠運行多個線程 - 這些線程將在給定的時間段內使用睡眠命令。我希望能夠在線程已經運行一段時間後,根據用戶輸入來操作這些睡眠線程的持續時間。如何訪問給定的Java線程
例如:
起價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;
}
}
}
所以基本上,我想要做的就是訪問對於我想要訪問的任何線程,classA中ClassB的setChangeSleepDuration
方法。這是可能的,如果是這樣,最好的方法是什麼?
感謝,