the cost of starting a new thread is higher because it involves interacting with the operating system, and when the program is creating a large number of short-lived threads, you should consider using a thread pool.
The thread pool creates a large number of idle threads at startup, can specify the number of threads, but a Runnable object is passed to the thread pool, and the thread pool initiates one of the threads to execute their run () method. When the run () method finishes executing, the thread does not die, but returns to the thread pool again to become idle. The run () method that waits for the next Runnable object to execute
The thread pool can set the maximum number of concurrent threads.
1. The disadvantage of new Thread
Perform an asynchronous task if it is a new Thread:
New Thread (New Runnable () {@Overridepublic void Run () {//TODO auto-generated Method stub}}). Start ();
This will have the following drawbacks:
A. Poor performance of new object each time it is newly Thread
B. Lack of unified management of threads, possible unlimited new threads, competing with each other, and potentially consuming excessive system resources leading to a crash
C. Lack of additional functionality, such as timed execution, periodic execution, thread interruption
Compared to the new thread, the benefits of the four thread pools provided by Java are:
A. Reusing existing threads, reducing the cost of object creation, extinction, performance, and reducing the overhead of CPU creation and elimination.
Like what:
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).
B. Can effectively control the maximum number of concurrent threads, improve the utilization of system resources, while avoiding excessive resource competition, avoid blocking
C. Provide functions such as timed execution, periodic execution, single-threaded, concurrency control, etc.
In Android, the introduction of thread pooling technology can greatly improve app performance when concurrent multiple network threads simultaneously
2. Java thread pool
There are two thread pool objects in Java that have a built-in thread pool executorservice----Represents a thread pool that executes threads as soon as possible (as long as there are idle threads in the thread pools, the thread task is executed immediately), Scheduledexecutorservice(scheduled means can be deferred after execution, is a subclass of Executorservice)
Newcachedthreadpool creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, and creates a new thread if it is not recyclable.
Newfixedthreadpool creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue.
Newscheduledthreadpool creates a fixed-line pool that supports timed and recurring task execution. The parameter represents the number of threads saved, even if the thread is idle and is saved in the thread pool.
Newsinglethreadexecutor creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).
Using the thread pool
The JDK itself has a thread pool implementation class Threadpoolexecutor
Executorservice provides three methods
1. Future<?> Submit (Runnable Task) submits a Runnable object to the specified thread pool, and the thread pool will have idle threads that are the tasks represented by the Runnable object. The future object represents the return value of the runnable task-but returns null after the execution of the run () method, but can invoke the Isdone (), iscancelled () method of the future to obtain the execution state of the Runnable object.
2. <T>future<t> Submit (Runnable task,t result) submits a Runnable object to the specified thread pool, and the thread pool will have idle threads that are the tasks represented by the Runnable object. Where the future object represents the return value of the runnable task-you can specify the return value that is displayed, and result is the return value after the thread execution ends.
Scheduledexecutorservice represents a thread pool that can execute thread tasks after a specified delay or periodically.
1, scheduledfuture<v> Schedule (Runnable command, long delay,timeunit unit)
Specifies that the command task will be executed after delay delays.
2,scheduledfuture<v> scheduleatfixedrate (Runnable command, long delay,long period,timeunit unit) Specifies that the command task will be executed after delay delays. and set the frequency of repeated execution, that is, in Delay+period, delay+2*period ... (repeat)
After you run out of a thread pool, you should call the shutdown () method of the thread pool, which initiates the shutdown sequence of the thread pool, which will no longer receive new tasks after calling the method, but will complete all previously submitted tasks. But after all the tasks in the thread pool are executed, all the threads in the pool die.
Java (Android) thread pool