Java self-brought thread pool Executors.newfixedthreadpool__java

Source: Internet
Author: User
Java self-brought thread pool Executors.newfixedthreadpoolAugust 29, 2014 ⁄ comprehensive ⁄ a total of 3920 characters ⁄ font size Small in Big ⁄ comment off

Sun in Java5, Java thread of the class library to do a large number of extensions, where the thread pool is one of the new features of JAVA5, in addition to the thread pool, there are many multi-threaded-related content, for multithreaded programming has brought great convenience. In order to write efficient, stable and reliable multithreaded programs, the new contents of the thread section are particularly important.

The contents of the threading feature of Java5 line are all under Java.util.concurrent, which contains a large number of interfaces and classes, and familiarity with this part of the API is a difficult learning process. At present, there are few information and books on this aspect, and the books on the thread of the large-owned introduction are still in the knowledge level before Java5.

Of course, the new features do not have to do with the long thread program, the Java5 before the General Assembly can write very good multithreaded programs. It's just a different price.

The basic idea of thread pool is also the idea of an object pool, which opens up a memory space, in which many (not dead) threads are stored, and the pool thread execution schedule is handled by the pool manager. When a thread task is taken from a pool, the threads object is returned to the pool after completion, which avoids the performance overhead of repeatedly creating the thread object and saves the resources of the system.

Before Java5, to implement a thread pool is quite difficult, now Java5 for us to do everything, we just need to follow the API provided to use, you can enjoy the great convenience of the thread pool.

The JAVA5 thread pool is divided into several types: the concrete can be classified into two kinds, fixed-size thread pool, variable size connection pool.

Before using the thread pool, you must know how to create a thread pool, in Java5, you need to know the Java.util.concurrent.Executors class API, which provides a number of static methods for creating connection pools that must be mastered.

One, fixed size thread pool, Newfixedthreadpool: [Java] view
Plain Copy

package app.executors;      import java.util.concurrent.executors;   import java.util.concurrent.executorservice;     /**   * java Threads: Thread pool    *    *  @author   Feng Xiaowei    */   public class test  {       public static void main (String[] args)  {            //  Create a thread pool of reusable fixed threads             executorservice pool = executors.newfixedthreadpool (5);            //  Creating Threads             thread t1 = new mythread ();            thread t2 = new mythread ();           &nBsp Thread t3 = new mythread ();            Thread t4 = new mythread ();            Thread t5 = new mythread ();           //   Execute the thread into the pool for execution            pool.execute (t1);           pool.execute (T2);            pool.execute (T3);           pool.execute (T4 );           pool.execute (T5);            //  Close thread pool             Pool.shutdown ();       }  }      class mythread  extends&nBsp thread {        @Override        public void  run ()  {           system.out.println ( Thread.CurrentThread (). GetName ()  +  "Executing ... ");       }  }  


Output results: [HTML] view
Plain Copy

Pool-1-thread-1 is executing ...   Pool-1-thread-3 is executing ...   Pool-1-thread-4 is executing ...   Pool-1-thread-2 is executing ... Pool-1-thread-5 is executing ...


Change the parameters in Executorservice pool = Executors.newfixedthreadpool (5): Executorservice pool = Executors.newfixedthreadpool (2), The output result is: [HTML] view
Plain Copy

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.