Question

I'm trying to do create several scheduled tasks in java.

However, when I add several tasks, some of them crash returning this exception:

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@219baf0b rejected from java.util.concurrent.ScheduledThreadPoolExecutor@74010c69[Shutting down, pool size = 2, active threads = 0, queued tasks = 4, completed tasks = 0]

This is the code I am using:

final ScheduledExecutorService schExService;

/**
 * Constructor per defecte de TaskSchedulker. Aquí s'inicaran els atributs i 
 * s'instanciaran els objectes necessaris per disposar d'un programador com 
 * el descrit totalement funcional.
 */
public TaskScheduler() {
    schExService = Executors.newScheduledThreadPool( 2 );
}
public void addTask(){
    final Runnable ob = new ExecutaFil(tskParams);
    schExService.schedule(ob, toSeconds( timeToWait ), TimeUnit.SECONDS);
}

And I'm triggering addTask method several times.

ExecutaFil is is just a class implementing Runnable with a sleep in it.

Any tip?

I tried changing the pool value (from 2 to for example, 100) and it's ignoring. Pool size is never higher than 4. I guess it's directly related to processor? How can I fix this?

Was it helpful?

Solution

rejected from java.util.concurrent.ScheduledThreadPoolExecutor@74010c69[Shutting down, pool size = 2, active threads = 0, queued tasks = 4, completed tasks = 0]

This means you shutdown the executor by calling shutdown() on it. If you want to keep adding tasks, don't shut it down.

I guess it's directly related to processor?

Nothing in the error message suggests this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top