Run a list of desktops with a time interval

I have a problem. in which I need to run a list of running objects with some delay in each query execution.

Say for example, I have a list below

List<MyReqObject> myReqObjects=new ArrayList<MyReqObject>();

and I created an artist with X number of threads as shown below

ExecutorService execute=Executors.newFixedThreadPool(X)

now with the help execute.invokeAl(myReqObjects);i am trying to call all these queries ...

but I have to have a delay between them. for this i tried

ScheduledExecutorService scheduler =Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(myObject, 2, 7, SECONDS); 

but here I can not send the list as an argument so that I can execute the same request within 7 seconds with a delay of 2 seconds ...

so there is a way to solve my problem please suggest me

+5
source share
3 answers

create timer:

Timer timer = new Timer();

if you need to run it once, then:

timer.schedule(new TimerTask() {
   @Override
   public void run() {
   // Your code here
   }
}, 2*1000); 

:

 timer.scheduleAtFixedRate(new TimerTask() {
   @Override
   public void run() {
   // Your code here
   }
}, 2*1000); 

. Timer TimerTask

+1

Runnable, Runnables, , . Runnable .

public class RunnableList implements Runnable {

    private List<Runnable> runList = Collections.synchronizedList(
                                                  new ArrayList<Runnable>());

    public void addRunnable(Runnable r) {
        runList.add(r);
    }

    @Override
    public void run() {
        if(!runList.isEmpty()) {
            runList.remove(0).run();
        }
    }   
}

, Runnable. : Executor, Callable.

0

. Runnables N N , .

.

int timeToWait = 2000;

ScheduledExecutorService scheduler =Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(myObject, 2, 7, SECONDS); 
for(int i = 1; i <= runnables.size(); i++){
   scheduler.schedule(r, timeToWait * i, TimeUnit.MILLISECONDS);
}

. runnable schedule it

final int timeToWait = 2000;
class SchedulingRunnable implements Runnable{
   private final Iterator<Runnable> runnables;
   private final ScheduledExecutorService scheduler

   public SchedulingRunnable(Iterator<Runnable> rs, ScheduledExecutorService es){..}

   public void run(){
      runnables.next().run(); //assuming runnables.hasNext()
      if(runnables.hasNext()){
          scheduler.schedule(this, timeToWait, TimeUnit.MILLISECONDS);
      }
   } 
}
ScheduledExecutorService scheduler =Executors.newScheduledThreadPool(1);
scheduler.schedule(new SchedulingRunnable(runnables.iterator(),schedule), timeToWait, TimeUnit.MILLISECONDS); 

In this example, the Runnable shell executes the next available one run, and then assigns the next available one after that with the specified timeout.

0
source

All Articles