how not to overwhelm java executorservice task queue?
===

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);
        //}
    } 

