Chapter thread pool

Chapter thread pool

2.1 Overview of the thread pool ideas

When we went to use threads to create a thread, so very easy to implement, but there will be a question:
  If the number of concurrent threads of many, and each thread is executed in a very short time the task is over, so often create threads will greatly reduce the efficiency of the system, because of frequent thread creation and destruction of threads takes time.
Is there a way so that the threads can be reused, that is, after executing a task not to be destroyed, but can continue to perform other tasks?
  In Java you can thread pool to achieve such an effect.

2.2 thread pool concept

  • Thread Pool: in fact, a receiving container multiple threads, which threads can be used repeatedly, often eliminating the need to create a thread object operation without repeatedly creates threads and consume too many resources.

  Because there are a lot of thread pool operations are related to the optimization of resources, we have here is not to repeat them. We pass a map to understand how the thread pool:

  

 

  

Rational use of the thread pool can bring three benefits:

  1. Reduce resource consumption. Reducing the number of creation and destruction of threads, each worker thread can be reused, you can perform multiple tasks.
  2. Improve the response speed. When the mission arrives, the task may not need to wait until the thread creation can be implemented immediately.
  3. Thread improve manageability. According to the capacity of the system, the number of adjustment line thread pool thread to prevent because of excessive consumption of memory, while the server burned himself (each thread takes about 1MB of memory, more threads open, also consume memory the greater the last crash).

2.3 Use of the thread pool

  Java thread pool inside the top-level interface is java.util.concurrent.Executor, but is not a strict sense Executor thread pools, but only one thread of execution tool. The real thread pool interface is a java.util.concurrent.ExecutorService.
  To configure a thread pool is more complex, especially for under the principle of the thread pool is not very clear, the likely configuration of the thread pool is not superior, thus providing a thread factory class inside java.util.concurrent.Executors some static factory to produce some common thread pool. The official recommended Executors engineering to create a thread pool object.
Executors class has created a thread pool as follows:

  • public static ExecutorService newFixedThreadPool (int nThreads): Returns the thread pool object. (Created is bounded thread pool, which is the number of threads in the pool can specify the maximum number)

To get a thread pool ExecutorService objects, how to use it, here we define a method using a thread pool object as follows:

  • public Future submit (Runnable task) <?>: get one thread pool thread object, and perform

  Future interface: used to record the result of a thread after the task is completed. Creating and using a thread pool.

 

Thread Pool: after providing the JDK1.5
java.util.concurrent.Executors: thread pool factory class used to generate the thread pool
static methods Executors class:
  static ExecutorService newFixedThreadPool (int nThreads) creates a fixed number of reusable threads thread pool
  parameters:
    int nThreads: create a number of threads in the thread pool contains
  the return value:
    ExecutorService interface class object is to achieve a return ExecutorService interface, we can use the interface to receive ExecutorService (oriented programming interface)
java.util.concurrent.ExecutorService: Interface thread pool
  is used to obtain the thread from the thread pool, start method is called, the task execution thread
    submit (Runnable task) Submits a Runnable task for execution
  shutdown method / destroy the thread pool
    void shutdown ()

Step (reduced) using the thread pool object:

  1. Create a thread pool object.
  2. Runnable interface to create a subclass object. (Task)
  3. Submit Runnable interface subclass object. (Take task)
  4. Close the thread pool (usually not).


Thread pool using the steps:

  1. Static method using a thread pool inside the factory class Executors provided newFixedThreadPool produce a specified number of threads the thread pool
  2. Create a class that implements the Runnable interface override the run method, set the thread task
  3. ExecutorService submit method calls in, passing threaded tasks (implementation class), open thread, run method
  4. The shutdown method call ExecutorService destruction thread pool (not recommended)

Code Example:

Demo01ThreadPool.java:

. 1  Import java.util.concurrent.ExecutorService;
 2  Import java.util.concurrent.Executors;
 . 3  
. 4  / * 
. 5      thread pool: after providing the JDK1.5
 . 6      java.util.concurrent.Executors: thread pool factory class, with generating a thread pool
 . 7      the Executors class static method:
 . 8          static ExecutorService newFixedThreadPool (int nThreads) to create a reusable fixed number of threads the thread pool
 9          parameters:
 10              int nThreads: create a number of threads in the thread pool comprising
 11          return value:
 12              ExecutorService interfaces, returns the class object implement ExecutorService interface, the interface receiving ExecutorService (oriented programming Interface) we can use
 13 is      java.util.concurrent.ExecutorService: Interface thread pool
 14         To obtain the thread from the thread pool, call start method, a thread of execution task
 15              Submit (Runnable Task) Submit a Runnable task for execution
 16          closing / destruction process thread pool
 . 17              void the shutdown ()
 18 is      using the thread pool steps:
 19          1. static method using a thread pool inside the factory class Executors provided newFixedThreadPool produce a specified number of threads in a thread pool
 20          2. create a class that implements the Runnable interface override the run method, set the threaded tasks
 21          3. call ExecutorService methods submit, transfer task thread (implementation class), a thread turn, run method
 22          4. call the shutdown method of destroying ExecutorService thread pool (not recommended)
 23 is   * / 
24  public  class Demo01ThreadPool {
 25      public  static  void main (String [] args) {
 26 is         // static method of using the thread pool class factory production newFixedThreadPool Executors provided inside a specified number of threads of thread pool 
27          ExecutorService Executors.newFixedThreadPool ES = (2 );
 28          // 3. submit ExecutorService the calling method, the thread transfer task (implementation class), open thread, run method 
29          es.submit ( new new RunnableImpl ()); // the pool-the thread-1-1 creates a new thread to perform
 30          // thread pool will remain open, ran out thread, the thread will automatically be returned to the thread pool thread can continue to use the 
31          es.submit ( new new RunnableImpl ()); // the pool-the thread-1-1 creates a new thread to perform 
32          es.submit ( new new RunnableImpl ( )); // the pool-2. 1-thread-creates a new thread of execution
 33 is  
34 is          //4. Call ExecutorService Methods shutdown destruction thread pool (not recommended) 
35          es.shutdown ();
 36  
37          es.submit ( new new RunnableImpl ()); // throw an exception, the thread pool is gone, you can not get a thread a 
38      }

 RunnableImpl.java:

1      / * 
2      2. Create a class that implements Runnable interface, override run method, provided threaded tasks
 . 3       * / 
. 4      @Override
 . 5      public  void run () {
 . 6          System.out.println (Thread.currentThread (). GetName ( ) + "to create a new thread of execution" );
 7      }

 

Guess you like

Origin www.cnblogs.com/116970u/p/11489317.html