directory

Several thread pools

Used to choose

implementation

Has a return value

There is no return value

Thread pool instantiation is used by analogy


introduce

This section describes the types, implementation modes, advantages and disadvantages of thread pools.

Java thread lock ReentrantLock non-fair lock ReentrantReadWriteLock Is a mutual-exclusive read and share lock

Yushen.blog.csdn.net/article/det…

Runnable Thread Callable < T > Future < String > FutureTask < String > Thread

Yushen.blog.csdn.net/article/det…

 

Several thread pools

 

  • Method signature Method Description Automatic destruction of an idle thread low-level implementation class
  • NewSingleThreadExecutor () does not destroy ThreadPoolExecutor by creating a thread pool with only one thread
  • NewFixedThreadPool (int nThreads) Creating a pool of a fixed number of threads does not destroy ThreadPoolExecutor
  • NewCachedThreadPool () creates a cacheable thread pool that can be incremented indefinitely. ThreadPoolExecutor is automatically destroyed after a thread is idle for 60 seconds
  • NewSingleThreadScheduledExecutor () to create a single thread pool with scheduling functions, can delay the task thread Don’t destroy ScheduledThreadPoolExecutor
  • NewScheduledThreadPool (int corePoolSize) creates a thread pool with a specified number of threads, with deferred scheduling. Don’t destroy ScheduledThreadPoolExecutor
  • NewWorkStealingPool () creates a ForkJoinPool of threads. The default number of threads is the current number of CPU cores and does not destroy the ForkJoinPool
  • NewWorkStealingPool (int Parallelism) Creates a ForJoinPool thread pool that does not destroy forkJoinPools

Used to choose

Depending on the need to use, Fixed number of commonly used, maximum memory usage, and service performance considerations

The Cached cache is increased wirelessly. Some threads occupy a small amount of space and can be used without any impact

Scheduled is used when you have Scheduled tasks

Consider the memory based on service analysis, such as performance, CPU, and number of in-memory database connections

 

implementation

Has a return value

import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; Public class test1 {public static void main(String[] args) {// Create a thread pool newCachedThreadPool = Executors.newCachedThreadPool(); / / thread by thread pool management MyCallable Future < String > Future = newCachedThreadPool. Submit (new t3 (98)); // If (! Future.isdone ()) {system.out.println (" Running! ); } try { System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } the finally {/ / close the thread pool newCachedThreadPool. Shutdown (); } } } class t3 implements Callable<String> { private int num = 0; public t3(int num) { this.num = num; } @Override public String call() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return num + "5578"; }}Copy the code

 

There is no return value

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; Public class test1 {public static void main(String[] args) {// Create a thread pool newCachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 3; i++) { newCachedThreadPool.execute(new t2(i)); } newCachedThreadPool.shutdown(); } } class t2 extends Thread { private int num = 0; public t2(int num) { this.num = num; } public int getNum() { return this.num; } @Override public void run() { for (int i = 0; i < 3; I ++) {system.out.println (num+" task executing "+ I); }}}Copy the code

 

Thread pool instantiation is used by analogy

  • The above several kinds of the thread pool, is such a method, the only change is the Executors. NewFixedThreadPool (9);
  •   ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
  •   ExecutorService newCachedThreadPool = Executors.newFixedThreadPool(9);
  • Submit with submit if there is a return value
  • No return value is executed using execute

 

 

Java thread lock ReentrantLock non-fair lock ReentrantReadWriteLock Is a mutual-exclusive read and share lock

Yushen.blog.csdn.net/article/det…

Runnable Thread Callable < T > Future < String > FutureTask < String > Thread

Yushen.blog.csdn.net/article/det…

 

 

 

 

 

ok

 

 

 

 

 

 

Continuously updated