Learn from the blogs of several great gods
The role of the thread pool:
The thread pool function is to limit the number of threads executing in the system.
According to the environment of the system, the number of threads can be set automatically or manually to achieve the best performance; less waste of system resources, more caused by the system congestion efficiency is not high. Use the thread pool to control the number of threads and other threads to wait. A task is completed, and then the first task from the queue is executed. If there is no waiting process in the queue, this resource of the thread pool is waiting. When a new task needs to run, it can start running if there are waiting worker threads in the thread pool, otherwise enter the wait queue.
public class Test {/** * Newsinglethreadexecutor * Creates a single thread pool. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. * If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted. */@org. junit.testpublic void Test () {Executorservice pool = Executors.newsinglethreadexecutor ();//thread t1 = new Mydata (); Thread t2 = new Mydata ();//place thread into pool for execution pool.execute (t1);p ool.execute (T2);//close thread pool Pool.shutdown ();} /** * Create a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. * The thread pool size will remain unchanged once the maximum value is reached, and if a thread ends up executing an exception, the thread pool will replenish a new thread. */@org. junit.testpublic void Yt () {Executorservice pool = Executors.newfixedthreadpool (2); Thread T1 = new Mydata (); Thread t2 = new Mydata ();//place thread into pool for execution pool.execute (t1);p ool.execute (T2);//close thread pool Pool.shutdown ();}
Thread two issues-----------the thread pool