A couple of questions regarding Java ExecutorService newFixedThreadPool

35
August 27, 2018, at 06:00 AM

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?

Answer 1

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)
READ ALSO
How to get progress status while uploading files to Google Drive using NodeJs?

How to get progress status while uploading files to Google Drive using NodeJs?

Im trying to get progress status values while uploading files to google Drive using nodeJs

39
JQuery Button id Selector is not working

JQuery Button id Selector is not working

I have multiple buttons to execute different queriesbut HTML is not capturing the event although button id is used as selector

9
Indicate current page on included Nav.php file

Indicate current page on included Nav.php file

I am trying to avoid code repetition by including my navigation links as a sperate file across all pages this works fine

23
How do I make it so only one audio file is playing and detect if one is already playing

How do I make it so only one audio file is playing and detect if one is already playing

My friends and I are not that good at coding and we have run into an issue whilst trying to play a random audio fileDon't judge please :

39