Hi,

I use the ThreadPoolTaskExecutor in order to execute parallel jobs. But when I apply a load I have frequently the TaskRejectedException.

My code is following :

my main thread put all tasks in the threadPoolTaskExecutor
Code:
    threadPoolTaskExecutor.setCorePoolSize(20);
    threadPoolTaskExecutor.setMaxPoolSize(20);
    threadPoolTaskExecutor.setQueueCapacity(1000);
    
     semaphore = new Semaphore(20);

     ..........

     pubic void apply(String message){
         semaphore.acquire();   // acquire or wait a semaphore 
         threadPoolTaskExecutor.execute(new MyTask(message));           
    }
I added a semaphore in order to manage the Queue. So it is impossible to have more than 20 in the Queue.


Code:
public class MyTask implements Runnable {
 
  public  MyTask (String message){

  }
  public void run() {
     // process a job : I update a database.
     semaphore.release(); // release the semaphore.
  }
}

Despite my semaphore I still have TaskRejectedException. It appears that the finished task is still in queue !

Could you help me because I don't see where is the problem.

Best Regards.

Chrisbry.