Q:

Internal Working of newFixedThreadPool

Kindly help me in understanding the internal flow of newFixedThreadPool (or Cached)
When we write below statements,
ExecutorService e=Executors.newFixedThreadPool(3);

e.execute(runaable1);
e.execute(runaable2);
e.execute(runaable3);
e.execute(runaable4);
e.execute(runaable5);

till 3 execute methods ,three threads will be created,when 4th execute method will be called,no new thread will be created but work will be waiting for a thread to be free. 
I do not understand this point "no new thread will be created but work will be waiting for a thread to be free." what i think when runnable1 will be given to first created thread,once runnable1's run method will be finished,Thread1's run will also be finished,thread1 will not be able to call run method of runnable4. 
So, how java manages to execute 5 Runnable with just 3 threads.

A:

FYI:  Here is a very simple implementation of a thread pool.
class MyThreadPool implements java.util.concurrent.Executor 
{
    private final java.util.concurrent.BlockingQueue<Runnable> queue;

    public MyThreadPool(int numThreads) {
        queue = new java.util.concurrent.LinkedBlockingQueue<>();
        for (int i=0 ; i<numThreads ; i++) {
            new Thread(new Runnable(){
                @Override
                public void run() {
                    while(true) {
                        queue.take().run();
                    }
                }
            }).start();
        }
    }

    @Override
    public void execute(Runnable command) {
        queue.put(command);
    }
}

This won't compile because I didn't deal with InterruptedException, and in a real thread pool, you would also want to deal with exceptions that might be thrown by the given command, but it should give you a rough idea of what a thread pool does.
It creates a queue and an arbitrary number of worker threads.  The worker threads compete with one another to consume commands from the queue.  The queue is a BlockingQueue, so the workers all sleep whenever the queue is empty.
Other threads may produce new commands (i.e., Runnable objects), and call the execute(command) method to put the new commands in the queue.  The commands will be performed (i.e., their run methods will be called) by the worker threads in approximately* the same order that they were enqueued.

Approximately, because worker A could pick a new command from the queue, and then lose its time slice before calling the command's .run() method.  Other workers could then pick other commands from the queue and perform them before the scheduler allows worker A to run again.

