Threads are created in the thread pool. Thread pool can improve performance very well. When a program passes a task to the thread pool, the thread pool will start a thread to execute the task. After execution, the thread will not die, but will return to the thread pool to become idle again and wait for the next task to be executed.
All four thread pools are created with Executors:
- Executors.newCacheThreadPool()
Create new threads on demand, first check whether there are previously established threads in the pool, and if so, reuse them. If not, build a new thread to join the pool, usually for short-lived tasks.
public class ThreadPoolExecutorTest { public static void main(String[] args) { //Create a thread pool ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { Thread.sleep(1000);//Create a thread every second cachedThreadPool.execute(new Runnable() { public void run() { //Printing Cache Thread Information Executing System.out.println(Thread.currentThread().getName()+"Being implemented"); } }); } } }
- Executors.newFixedThreadPool(int n)
Create a thread pool with a fixed number of concurrent threads. After a thread completes the current task, the thread can be reused to perform another task. When n threads in the thread pool are occupied, other tasks wait in the queue according to the order of task submission.
public class ThreadPoolExecutorTest { public static void main(String[] args) { //Create a reusable fixed number of thread pools ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; i++) { fixedThreadPool.execute(new Runnable() { //Very powerful anonymous inner classes public void run() { try { //Printing Cache Thread Information Executing System.out.println(Thread.currentThread().getName()+"Being implemented"); Thread.sleep(2000); //Print the names of three threads in turn every 2 seconds } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
- Executors.newSingleThreadExecutor()
Create a single thread pool that only uses a single thread to perform tasks. If the only thread ends abnormally, a new thread will replace it. This thread pool ensures that all tasks are executed in the order in which they are submitted.
public class TestThreadPoolExecutor { public static void main(String[] args) { //Create a single threaded thread pool ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { /*index As an invariant, after each for loop, the final variable gives the scope of the block. It will fail and rebuild a final variable in different domains in the next loop, so the corresponding threads in different domains will use it. Different index es*/ final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { //The results are output sequentially, which is equivalent to performing tasks sequentially. System.out.println(Thread.currentThread().getName()+"Being held //Line, the value printed is: "+index"; Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
- Executors.newScheduledThreadPool(int n)
To create a fixed-length thread pool, there are two kinds of task execution in the thread pool: regular or periodic execution.
- Timely execution
public class ThreadPoolExecutorTest { public static void main(String[] args) { //Create a fixed-length thread pool to support timed and periodic task execution -- delayed execution ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); //Delay 1 second execution scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println("Delay 1 second execution"); } }, 1, TimeUnit.SECONDS);//1 Execute once in seconds. over } }
- Periodic execution
public class ThreadPoolExecutorTest { public static void main(String[] args) { //Create a fixed-length thread pool to support timed and periodic task execution -- periodic execution ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); /*Delay 1 second to execute the task, then execute the task again every 3 seconds, if the last task has been completed every 3 seconds Start the mission, or wait until the last mission is over (although it's already three seconds)*/ scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println("Delay 1 second and execute every 3 seconds"); } }, 1, 3, TimeUnit.SECONDS); } }