On application, i'm using
Executor executionContext = Executors.newFixedThreadPool(10);
for fixed the 10 threads.
I don't want to fix the number of thread, I want thread will be dynamic. The number of threads it requires, it will process.
How it can be applied?
The 10 in your question is the value of the thread pool, not the number of threads. You say that you want:
The number of threads it requires, it will process.
Then your code will work as you want. It will use as many threads as it needs until it reaches the thread pool value. You can also choose what happens when it reaches the max thread pool using "rejection policy".
Related
I have a 3 instance of a class which is implementing runnable interface. I am instantiating my Executor class like below;
executor = Executors.newScheduledThreadPool(2);<--- talking about this part
executor.scheduleAtFixedRate(unassignedRunnable, 0, refreshTime, TimeUnit.SECONDS);
executor.scheduleAtFixedRate(assignedToMeRunnable, 2, refreshTime, TimeUnit.SECONDS);
executor.scheduleAtFixedRate(createTicketsFromFile, 3, refreshTime * 2, TimeUnit.SECONDS);
My question is, Does it make any difference, if I change thread pool count from 2 to 1 or 3 ? I tried and gained nothing almost. Can anyone explain the real use of thread pool count ? Maybe my tasks are lightweight ?
You need to understand, it doesn't matter how many threads you are going to create, ultimately, threads would be executed upon number of available cores. Now, as per documentation, it is "the number of threads to keep in the pool, even if they are idle."
Can you tell me what is the real use of thread pool count
executor = Executors.newScheduledThreadPool(2);
Above line of code will create 2 threads in thread pool , but it doesn't mean all will be doing some work. But, on same time, the same thread can be used to perform some other task from thread pool, which was submitted.
So, it is better to understand your requirement before picking the total number of threads to be created. (I usually prefer the number, depending on the number of available cores count)
That is corePoolSize is number thread in pool .Available thread pick the eligible task and run in same thread.If there is no thread available though task is eligble for run will not execute as all threads are busy.In your case may be your tasks very short lived.To demo create corepool size one and submit long running task and after that submit a light task check the behavior then increase the corepoolsize to 2 and see the behavior.
It depends on number of CPU cores of a machine, on which you are running your application. If you have more number of CPU cores, multiple threads can run in parallel and performance of overall system can be improved if your application is not IO Bound application.
CPU bound application will benefit with more number of cores & threads.
If you have 4 core CPU, you can configure the value as 4. If your machine has single CPU core, there won't be any benefit to change the pool size as 4.
Related SE questions:
Java: How to scale threads according to cpu cores?
Is multithreading faster than single thread?
I wanted to know whether there is a way for increasing number of threads in FixedThreadPool or ScheduledThreadPool when all the threads are in usage.
That is for Example1: suppose we take a FixedThreadPool with size say 5, when all the threads are in usage then another task or function which need to be done has to wait for a thread to become free. I need to avoid this and the number of threads has to increase dynamically without using CachedThreadPool.
Is there any way in doing so?
Example2:
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
I have limited the number of threads in the above example 2 to 1. Now when this thread is busy in doing some task the other task to be done has to wait right?
Now I want like : this fixed number of threads has to increase dynamically.
Is there anyway in doing so?
There is actually only one class which implements ScheduledExecutorService called the ScheduledThreadPoolExecutor. This has two parameters to define the number of threads it can have which is the core thread pool (or minimum size) and the maximum thread pool size.
The fixed thread pool factory method creates an instance of this class where the minimum and maximum are the same.
The caches thread pool factory method creates an instance which has a minimum of 0 and no real maximum.
If you want a dynamic thread pool size I suggest you set a high maximum and a minimum you think is appropriate. Whether you create the instance directory or used the fixed/cached thread pool doesn't make much difference.
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
No. Here ScheduledExecutorService will not spawn new threads.
you have to go for newCachedThreadPool
I have a long running process that listens to events and do some intense processing.
Currently I use Executors.newFixedThreadPool(x) to throttle the number of jobs that runs concurrently, but depending of the time of the day, and other various factors, I would like to be able to dynamically increase or decrease the number of concurrent threads.
If I decrease the number of concurrent threads, I want the current running jobs to finish nicely.
Is there a Java library that let me control and dynamically increase or decrease the number of concurrent threads running in a Thread Pool ? (The class must implement ExecutorService).
Do I have to implement it myself ?
Have a look at below API in ThreadPoolExecutor
public void setCorePoolSize(int corePoolSize)
Sets the core number of threads. This overrides any value set in the constructor.
If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle.
If larger, new threads will, if needed, be started to execute any queued tasks.
Initialization:
ExecutorService service = Executors.newFixedThreadPool(5);
On need basis, resize Thread pool by using below API
((ThreadPoolExecutor)service).setCorePoolSize(newLimit);//newLimit is new size of the pool
Important note:
If the queue is full, and new value of number of threads is greater than or equal to maxPoolSize defined earlier, Task will be rejected.
So set values of maxPoolSize and corePoolSize properly.
So I want to change the fixedthreadpool size after already declaring it before, how would I go about doing this. I tried this but it doesn't work:
ExecutorService pool = Executors.newFixedThreadPool(3);
//pool has 21 threads in queue
//do somestuff
network sends a change in thread limit signal when there are 12 threads left in queue
//redeclare:
pool = Executors.newFixedThreadPool(4);
But it doesn't work, only 3 threads continue to be excuted at a time.
I think I know what the problem is: the executor stores the 21 threads in queue and wont change the fixed thread pool size till the queue is cleared.
I don't want this, How would I make it so that the change in threadpoolsize affects the entire queue? Would I need to reload the entire queue (if so, How would i do that)?
Creating a new pool does get rid of the old one, unless you shut it down you now have two pools, one with three threads and one with four.
The simplest thing to do is to create a ThreadPoolExecutor (which is the implementing class here) This will allow you to change the core pool size and the maximum pool times.
// start with a pool with a min/max size of 3.
ThreadPoolExecutor executor = new ThreadPoolExecutor(3,3,60, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
// set the min/max size to 4.
executor.setMaximumPoolSize(4);
executor.setCorePoolSize(4);
Another option is to make the pool size 4 from the start if you know that 4 is reasonable.
Will the thread creating method wait for a thread to get free?
can I reduce the number of threads generated using thread pooling?
If you use a cached thread pool, the pool will create more threads. However this will only be the maximum needed at any one time and might be far less than the number of tasks you submit.
If you use a fixed size thread pool, it will create a fixed number of threads regardless of whether you give it any tasks, or if you give it more tasks than it can do. It will queue any tasks which are waiting.
Will the thread creating method wait for a thread to get free?
While you could create a queue which did this, this is not the default behaviour. A more common solution is to make the caller execute the task if this is required.
can I reduce the number of threads generated using thread pooling?
Thread pooling is likely to produce far less threads than tasks esp if you limit the number of threads.
Will the thread creating method wait for a thread to get free?
That contradicts with your title. You'd normally submit a task and the pool would pass that task to a worker thread when one is available. So you'd not create a thread but submit a task. Whether you wait for the task to be executed or just trigger asynchronous execution (which in most cases would be the default) depends on your system and requirements.
Can I reduce the number of threads generated using thread pooling?
Thread pooling is often used to reduce the number of threads created, i.e. instead of having a thread per task you have a defined (maximum) number of worker threads and thus if #tasks > max threads in pool you'll reduce the number of threads needed.
From the ThreadPoolExecutor documentation:
A ThreadPoolExecutor will automatically adjust the pool size (see
getPoolSize()) according to the bounds set by corePoolSize (see
getCorePoolSize()) and maximumPoolSize (see getMaximumPoolSize()).
When a new task is submitted in method execute(java.lang.Runnable),
and fewer than corePoolSize threads are running, a new thread is
created to handle the request, even if other worker threads are idle.
If there are more than corePoolSize but less than maximumPoolSize
threads running, a new thread will be created only if the queue is
full. By setting corePoolSize and maximumPoolSize the same, you create
a fixed-size thread pool. By setting maximumPoolSize to an essentially
unbounded value such as Integer.MAX_VALUE, you allow the pool to
accommodate an arbitrary number of concurrent tasks. Most typically,
core and maximum pool sizes are set only upon construction, but they
may also be changed dynamically using setCorePoolSize(int) and
setMaximumPoolSize(int).
Basically, you can set two sizes: the 'core' size and the 'max' size. When tasks are submitted, if there are fewer than 'core' threads, a new thread will be created to execute that task. If there are greater than 'core' threads, one of the current threads will be used to execute tasks, unless all current threads are busy. If all current threads are busy, more threads will be created up to 'max' size. Once 'max' number of threads are reached, no more will be created, and new tasks will be queued until a thread is available to run them.
In the general case, there is no 'right' way that thread pools work. Any given implementation could be used: a fixed size thread pool that always has X threads, or a thread pool that always grows up to a maximum limit, etc.
ThreadPoolExecutor's submitted method throws RejectedExecutionException if the task cannot be scheduled for execution.