Basically, I want to be able to run multiple threads – these threads will use sleep commands for a given period. I want to be able to manipulate the duration of these sleep threads based on user input after the thread has already been running for a period.
For example:
Starting the thread from 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 starts 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;
}
}
}
So basically, what I want to do is access the setChangeSleepDuration method in ClassB from classA for any thread that I want to access. Is this possible and if so what is the best way?
Thanks,
I suppose that
jobThreadInterface.create(job, objectList)does create an instance ofClassB. In that method, you could store the reference toClassBin a collection that you can access later.So something like:
And later in your code:
Or you could store the runnables in a map to associate them with some keys that will help you retrieve them later on.