ShinKorean
ShinKorean

Reputation: 9

A couple of questions regarding Java ExecutorService newFixedThreadPool

Please note that I usually ask a question after googling for more than 20 times about the issue. But I can't still understand it. So I need your help.

Basically, I don't understand the exact usage of newFixedThreadPool

  1. Does newFixedThreadPool(10) mean having ten different threads? Or does it mean it can have 10 of the same threads? or the both? I executed with submit() methods more than 20 times and it's working.

  2. Does submit() print a value? Or are you putting threads in the ExecutorService?

Upvotes: 0

Views: 1227

Answers (2)

Rama Sharma
Rama Sharma

Reputation: 116

Just refer java docs or JAVA API's description rather than googling it. For your questions I have below comments .

Question 1 ->

ExecutorService executorService = Executors.newFixedThreadPool(10);

First an ExecutorService is created using the Executors newFixedThreadPool() factory method. This creates a thread pool with 10 threads executing tasks.

Executors.newFixedThreadPool API creates, a thread pool that reuses a fixed number of threads and these threads work on a s***hared unbounded queue***. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly SHUTDOWN. After submitting even 20 tasks ,it worked with this thread pool.

Internally it calls below line of codes . public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); }

Question 2- > Submits a Runnable task for execution in Queue and it can also return an Object of type Future Object representing task. we can use Future's get method to check whether submitted task has successfully completed or not because it will return null upon successful completion.

Upvotes: 0

Brooke
Brooke

Reputation: 21

Briefly, tasks are small units of code that could be executed in parallel (code sections). The threads (in a thread pool) are what execute them. You can think of the threads like workers and the tasks like jobs. Jobs can be done in parallel, and workers can work in parallel. Workers work on jobs.

So, to answer your questions:

  1. newFixedThreadPool(int nThreads) creates a thread pool of nThread threads that operate on the same input queue. nThreads is the maximum number of threads that can be running at any given time. Each thread can run a different task. With your example, you can be running up to 10 tasks at the same time. (The documentation can be found here with credit to @hovercraft-full-of-eels)
  2. submit() pushes the given task into an event queue that is shared by the threads in the thread pool. Once a thread is available, it will take a task from the front of the queue and execute it. It shouldn't print anything, unless the Runnable you pass it has a print statement in it. However, the print statement may not be printed right when you submit the task! It will print once a thread is executing that particular task. (The documentation can be found here)

Upvotes: 2

Related Questions