Thread two issues-----------the thread pool

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.