I have the below code snippet, which runs fine. But the problem is it creates and put over 2000 tasks on the executor queue right off the bat.
I need to have a check if the tasks already in the executor queue are complete, only then give it more tasks. It doesnt have to be exact, ie if the queue has <10 tasks left, add 50 more.
So the executor task queue doesnt have so many pending tasks, which will also allow shutdown() to work in a timely manner, otherwise even if called, the executor will still trying to complete all 2000 tasks in its queue first.
What is the best way to accomplish this? thank you
executor = Executors.newFixedThreadPool(numThreads);
while(some_condition==true)
{
//if(executor < 10 tasks pending) <---- how do i do this?
//{
for(int k=0;k<20;k++)
{
Runnable worker = new MyRunnable();
executor.execute(worker);
}
//}
//else
//{
// wait(3000);
//}
}
Update using semaphore:
private final Semaphore semaphore = new Semaphore(10)
executor = new ThreadPoolExecutorWithSemaphoreFromJohnExample();
while(some_condition==true)
{
Runnable worker = new MyRunnable();
//So at this point if semaphore is full, then while loop would PAUSE(??) until
//semaphore frees up again.
executor.execute(worker);
}