0

Executors.newFixedThreadPool(5) creates 5 threads in the pool, and then in the loop another 100 threads are created. Is this understanding right? Then the 5 threads in the pool will execute each thread in the queue of 100 worker thread.

so in total there are 105 threads created? I had thought only 5 threads are created, but each Runnable is also a thread.

ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 100; i++) {
    Runnable worker = new WorkerThread("" + i);
    executor.execute(worker);
}
executor.shutdown();
Jason C
  • 38,729
  • 14
  • 126
  • 182
ling
  • 1,555
  • 3
  • 18
  • 24

5 Answers5

2

A Runnable is not a running thread. Neither is a Callable nor a Thread for that matter.

A thread is only created at the JVM level when you .start() a Thread (which will .run() a Runnable or .call() a Callable); until then, they are just normal, regular objects.

As you use an Executor, it is the executor which will manage your Runnables or Callables and start "real" threads when it can do so.

fge
  • 119,121
  • 33
  • 254
  • 329
2

Executors.newFixedThreadPool(5) creates 5 threads in the pool, and then in the loop another 100 threads are created. Is this understanding right? Then the 5 threads in the pool will execute each thread in the queue of 100 worker thread.

This is not correct. The fixed-size thread pool executor will create 5 worker threads. Your 100 Runnables are all added to a queue that these 5 threads are pulling from. So only 5 of your Runnables are executing at any given time. There are 5 threads (plus the main thread, and possibly the EDT if you're using Swing, and of course any other unrelated threads that you've explicitly created).

I had thought only 5 threads are created, but each Runnable is also a thread.

A Runnable is not a thread. A Runnable, as its name implies, is just something that can be run. In the case of the fixed-size thread pool, it is being run by one of the threads in the pool. From the documentation for Runnable (emphasis mine):

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

I believe the source of your confusion may be that your Runnables are innapropriately named WorkerThread, even though they are not threads. Additionally, you may be confused by the fact that Thread also implements Runnable. In fact, this literally means that a Thread is a Runnable, not that a Runnable is a Thread -- and a Thread isn't even a thread until it's started.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • The WokerThread is doing its real job inside executed by one of the 5 threads. If I want to keep a total of how many tasks are done by the 5 threads, how should I do? – ling Mar 17 '14 at 20:01
  • @CongminMin For that you would have to maintain a count somewhere and have each task increment the count when it's done. There are many ways to implement that; all would involve passing an instance of some object that maintains a count to the `WorkerThread` constructors (or setting via some other method before putting the tasks in the pool). Essentially you have to have them call some "callback" method when they're finished that increments a counter somewhere. The executor itself does not provide a way to determine the number of tasks executed. – Jason C Mar 17 '14 at 20:04
1

Don't confuse the Thread class with a thread. Java uses the Thread class to spawn threads, but it's not necessarily a 1-to-1 mapping.

In your code, it seems your WorkerThread class is a sub class of Thread. However, Thread also implements Runnable and that's how your ExecutorService is using it. It just invokes its run() method within one of the 5 threads that it started.

how many threads are created in this code?

All the threads started by the ExecutorService.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

A Runnable is just an interface like any other interface. Also Thread is just a class not a separate thread. An actual thread is not created until you call the start() method on Thread class.

Sourabh Bhat
  • 1,793
  • 16
  • 21
0

"Number of threads in pool will be fixed. If requests come more than the number of threads, they will wait in queue. If a thread dies or terminated, a new thread will be created." More here

In this case, only maximum number of 5 threads would be exist.

Community
  • 1
  • 1
RoastDuck
  • 124
  • 2
  • 10