Java- fourth in multi-threaded thread pool

1 What is the thread pool.

    Thread pool is to create a large number of idle threads in the system startup, the program will be a Runnable object or Callable object passed to the thread pool, thread pool will start a thread to perform their run () or call () method, when run after the execution () or call () method, the thread does not die, but returned again to the thread pool called an idle state, awaiting execution run next Runable object () or call () method.

    Java 5 previously required for manual thread pool, the thread pool built Java 5, the new Excutors factory class to create a thread pool, the factory class includes the following static factory methods to create a thread pool:

  7 above method, the first three returns a ExecutorService object that represents a thread pool, which can perform a thread Runnable object or Callable object represented; intermediate 2 returns an ScheduledExecutorService thread pool, which is ExecutorService subclass it can be executed after a specified delay-threaded task; the last two are Java 8 new method, the ability to take full advantage of multi-CPU parallel. These two methods generated work stealing pool, are the equivalent of a background thread pool, if all foreground threads have died, the thread pool will automatically work stealing death.

    ExecutorService representatives as soon as possible the implementation of thread pool threads (as long as there is a free thread pool thread, a thread of execution on the task immediately), offers three methods:

   

   On behalf of ScheduledExecutorService may delay or periodically during the specified task execution thread pool thread, provides four methods:

   

   After using a thread pool thread pool should call the shutdown () method, which will close the thread pool to start the sequence. After calling this method thread pool will no longer receive new tasks, but will have to submit all the previous task execution is completed. After all tasks have been submitted are executed, the thread pool threads will all die. The calling thread pool shutdownNow () method to close the thread pool, will try to stop all actively executing tasks, halts the processing task is waiting, and returns a list of tasks awaiting execution.

   To use the thread pool are as follows:

    1 "to call a static factory method to create a ExecutorService Executors class object that represents a thread pool.

    2 "Creating a class that implements Runnable or Callable instance of a class, the task as a thread of execution.

    3 "call ExecutorService object submit () method to submit Runnable object or Callable object.

    4 "When you do not want to submit any task, call the shutdown ExecutorService object () method to close the thread pool.

    for example:

public  class ImplCallable the implements a Callable <Integer> { 

   ... 
   @Override 
   public Integer Call () { 
 
      ... 
      // do something 
      System.out.println (Thread.currentThread () getName ().);
       // return value A. 
      return . 1 ; 
   } 

    public  static  void main (String [] as AGRS) throws InterruptedException, ExecutionException { 

      ... 
// Create a thread pool having a fixed number of threads (6)
ExecutorService = Executors.newFixedThreadPool the pool (6);
// create and start a thread FutureTask <Integer> target =new new a FutureTask, <Integer> ( new new ImplCallable ()); // submitted to two thread pool threads
pool.submit (target);
pool.submit (target);
// Close thread pool
pool.shutdown (); } }

2, Java 8 enhanced ForkJoinPool

 

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11409913.html