ThreadPool

                                        
   What really the ThreadPool Is…

A pool
Threadpool:- As its name resembles a pool of threads. It contains some threads, which it reuses, again and again, to accept task submitted to it and run it in one of the thread.
So to describe the real requirement of a thread pool, I have mentioned a conversation between two guys…

Harold : Hey Kumar, my app starts lagging and sometimes show ANR dialog…

Kumar: There could be the problem of Memory leak in your Application…

Harold: Not, I haven’t found any memory leak in my app, I am also doing all my work in the background…

Kumar: So what are you using, to perform your task in Background.

Harold: I am creating a separate Thread whenever there is any need to perform the task in the background.

Kumar: There you are… Bro creating new Thread again and again may lead to Thread overhead.

Harold: So we aren’t allowed to create separate Thread to do some work.

Kumar : Bro I am not saying that , what I mean is that if we create new thread it takes space in memory(CPU allow our some heap to program and some space from that heap is allocated to us ) and our app has very limited amount to space allocated to CPU, and when thread is no longer used GC will be called to free up the space . So more thread you create more fast Garbage collection occur. And Gc isn’t in control of programmer, If garbage collection occurs in the middle of an intensive processing loop like an animation or during music playback, it can increase processing time, the main thread cannot finish executing blocks of work within 16ms, the user may observe hitching, lagging, or a lack of UI responsiveness to input.

Harold: Okk…So is there any way to do my work in separate thread…

Kumar: Yes, You Can Go to Thread Pool. Thread pool creates only a few number of threads, and then reuse them again and again to complete the tasks submitted by the user.

Harold: So how many of thread will it create and how can we put our work in its threads….and what are its advantages…

Kumar:
1. Thread pool allows us to create a number of threads we want to create. And Then it uses these threads to do our background tasks.
2. Thread pool also manages synchronization of threads by its own.
3. We can do work in parallel in two separate threads.
4. We can also do prioritization among Threads.
5. Even for a small task, you can use a thread pool, because it will not create a new thread, it will perform the work in the existing threads.

we can use Threadpool, for every single task.You just have to initialize the singleton class of thread pool and when you want to do some task in a separate thread just pass your runnable in the thread pool, it will execute it in one of its threads, and you can get the data or notify when work complete in 
UI using Handler.

Now we will see how to implement it…

Threadpool Executor is a class which allows us to use thread pool easily.
Give below is the Constructor of ThreadPoolExecutor 
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler);

Now let me explain you each signature one by one:-

Core pool size:-the number of threads that we need to have in thread pool in the first place. Initially, there are no threads in the thread pool.

MaximumPoolSize:- the maximum number of threads that we want to have in our pool.

WorkQueue:- it this the queue that will hold the runnable, which will be submitted to the thread pool and when any of the thread pool thread ask for the task, this queue will provide that task to the thread. This is of BlockingQueue type.

KeepAliveTime:- it is time up to which free threads will wait to get a new task if the thread wouldn’t get the task within KeepAliveTime, the thread will terminate.

Unit:- unit of time for KeepAliveTime.

ThreadFactory:- it helps thread pool to create a new Thread on demand.we Can also customize it but we will see it later.

RejectedExecutionHandler:- it is the handler to handle the exception RejectedExecutionException.

RejectedExecutionException:- this is the exception that workQueue will throw when the queue is full and still we try to insert runnable in it.
Here is how we can create our own RejetedExecutionHandler implementation to handle jobs that can’t fit in worker queue: —




Now here is the implementation of Threadpool executor:-



My runnable class that has to pass in the executor

You can find the Sample Code of Threadpool from here:-

GitEliteNovice/pool of threads

If you want to know more about Threadpool you can refer here…

Threadpoolexecutor By Mindorks

I have learned lots of things from Mindorks, they are really fabulous…

If you like my hard work and effort, give it share…

You can also find me on..

GitHub , FacebookQuora , Twitter

Comments

Popular posts from this blog

Please Don’t Bother Yourself to bother others