How to access a given Java thread

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?



I suppose that jobThreadInterface.create(job, objectList) does create an instance of ClassB. In that method, you could store the reference to ClassB in a collection that you can access later.

So something like:

ClassB runnable = jobThreadInterface.create(job, objectList);

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.

You could keep a hold of the Job instance. Then when the thread starts working on the job, save the thread as an attribute of your job. Then classA already knows the Job, so it can access the thread.

Alternatively, you might simply want to save that changeSleepDuration value in the job itself. It comes down to semantics. What does changeSleepDuration represent (a thread control, or is it part of the job?). Sounds like the latter. I'd go for option two.

Need Your Help

Minimax algorithm in a game

algorithm artificial-intelligence minimax

Im trying to create my first game using the minimax algorithm but i dont know how to implement this using a tree.