1. Introduction of Thread pool
Benefits of Introduction:
1) Improve performance. Create and consume objects cost-consuming CPU resources
2) Prevent excessive memory consumption. Controls the number of active threads, preventing too many concurrent threads.
Conditions of Use:
Assume that the time to complete a task on a single server is T
T1 when the thread was created
T2 time to execute a task in a thread, including the time required to synchronize between threads
T3 the time the thread was destroyed
Obviously t = t1+t2+t3. Note that this is an extremely simplified hypothesis.
It can be seen that T1,T3 is the overhead of multithreading itself, and we are eager to reduce the time it takes to t1,t3, thus reducing t time. However, users of some threads do not notice this, so the threads are created or destroyed frequently in the program, which causes T1 and T3 to occupy a considerable proportion in T. Obviously this is the highlight of the thread's weakness (T1,T3), not the merit (concurrency).
Thread pooling technology is a technique that focuses on how to shorten or adjust t1,t3 time to improve the performance of server programs. It t1,t3 the start and end of the server program, or some idle time period, so that there is no t1,t3 overhead when the server program processes the client request.
The thread pool not only adjusts the time period generated by the T1,T3, but it also significantly reduces the number of threads created.
In Android, the introduction of thread pooling technology can greatly improve the performance of your app when concurrent with multiple network threads.
2. Thread Pool Example
1) The JDK itself has a thread pool implementation class Threadpoolexecutor
2) Here is an example of a simulated threadpoolexecutor to deepen the understanding of the principle
PublicFinalclassThreadPool {//the number of default threads in the thread pool is 5 Private Static intWorker_num =5; //Worker Threads Privateworkthread[] workthreads; //task queue, as a buffer, the list thread is unsafe PrivateList<runnable> Taskqueue =NewLinkedlist<runnable>(); Private StaticThreadPool ThreadPool; //Create a thread pool with the number of default threads PrivateThreadPool () { This(5); } //Create a thread pool, worker_num the number of worker threads in the thread pools PrivateThreadPool (intworker_num) {Threadpool.worker_num=Worker_num; Workthreads=NewWorkthread[worker_num]; for(inti =0; i < Worker_num; i++) {Workthreads[i]=NewWorkthread (); Workthreads[i].start ();//to open a thread in a thread pool } } //single mode, get a thread pool with the number of default threads Public StaticThreadPool Getthreadpool () {returnGetthreadpool (Threadpool.worker_num); } //single mode, get a thread pool with a specified number of threads, Worker_num (>0) is the number of worker threads//worker_num<=0 Create default number of worker threads Public StaticThreadPool Getthreadpool (intworker_num1) { if(ThreadPool = =NULL) ThreadPool=NewThreadPool (WORKER_NUM1); returnThreadPool; } //To perform a task, it is simply to join the task queue and when to execute the thread pool manager perception Public voidAddTask (Runnable Task) {synchronized (taskqueue) {taskqueue.add (Task); Taskqueue. Notifyall (); } } //destroys the thread pool, which ensures that all threads are destroyed if all tasks are completed, otherwise it is not destroyed until the task is completed Public voiddestroy () { while(!taskqueue.isempty ()) {//If there's still a job to do, just go to sleep. Try{Thread.Sleep (Ten); } Catch(interruptedexception e) {e.printstacktrace (); } } //worker thread stops working and is set to null for(inti =0; i < Worker_num; i++) {workthreads[i].stopworker (); Workthreads[i]=NULL; } ThreadPool=NULL; Taskqueue.clear ();//Empty Task Queue } /** * Inner class, worker thread*/ Private classWorkthread extends Thread {//whether the worker thread is valid to end the worker thread PrivateBoolean isrunning =true; /** The key is that if the task queue is not empty, then the task execution is removed, and if the task queue is empty, wait*/@Override Public voidrun () {Runnable R=NULL; while(isrunning) {//Note that if the thread is invalid, the Run method ends naturally, and the thread is useless.synchronized (taskqueue) { while(IsRunning && taskqueue.isempty ()) {//queue is empty Try{taskqueue.wait ( -); } Catch(interruptedexception e) {e.printstacktrace (); } } if(!Taskqueue.isempty ()) R= Taskqueue.remove (0);//Remove Task } if(r! =NULL) {r.run ();//Perform Tasks} R=NULL; } } //stop working and let the thread run through the Run method naturally ends Public voidStopworker () {isrunning=false; } }}
Android Thread pool Depth analysis