Several thread pools

Method signature Method Description Automatic destruction of an idle thread low-level implementation class

ThreadPoolExecutor newFixedThreadPool(int nThreads) Threadpool created with a fixed number of threads will not be destroyed ThreadPoolExecutor newCachedThreadPool() creates a cacheable thread pool with an infinitely increasing number of threads. Automatically destroyed ThreadPoolExecutor newSingleThreadScheduledExecutor () to create single thread pool with scheduling functions, Tasks can delay the thread Not destroy ScheduledThreadPoolExecutor newScheduledThreadPool (int corePoolSize) create a specified number of threads of the thread pool, has a delay scheduling function. Don’t destroy ScheduledThreadPoolExecutor newWorkStealingPool create ForkJoinPool thread pool (), The default number of threads is the current number of CPU cores and will not be destroyed by ForkJoinPool newWorkStealingPool(int Parallelism). Specifies the number of threads that will not be destroyed by ForkJoinPool usage. The number of threads that can be used by ForkJoinPool is selected based on the need to use, the Fixed number that is commonly used, the maximum memory usage that is controlled, 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

 

The 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 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 (); }}Copy the code

}

class t3 implements Callable { 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();

}
Copy the code

}

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 to instantiate the lines using the above several kinds of thread pool, both methods, the only change is the Executors. NewFixedThreadPool (9); ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); ExecutorService newCachedThreadPool = Executors.newFixedThreadPool(9); Submit with a return value and execute with no return value