Common thread pools in Java

Tinker Bell is not lazy 2022-02-13 09:09:07 阅读数:12

common thread pools java


Short step , A thousand miles ; Don't product the little stream , Beyond into the sea . Let's settle down , Poetry and travel expenses from afar are really expensive !

Java Common thread pools in

type

  • newCacheThreadPool
  • newFixedThreadPool
  • newSingleThreadPool
  • newScheduleThreadPool

difference

  1. newCacheThreadPool
  • Is a thread pool with a cache , Cache each thread ( The default cache release time is that the worker thread is idle for one minute ), The number of worker threads is infinite .
  1. newFixedThreadPool
  • When creating a thread pool, fix the maximum number of working threads , When there are many tasks , The worker thread has reached the upper limit , The task enters the waiting queue , Wait for a thread to finish executing .
  1. newSingleThreadPool
  • Ensure that there is only one worker thread in the thread pool , The tasks are executed in sequence .
  1. newScheduleThreadPool
  • Timing can be performed , Delay , Periodic tasks .

Use

  1. newCacheThreadPool

Cacheable thread pool . Cache threads , Reuse threads , Reduce the consumption of resources . When the last task is over , Reuse the current thread to perform tasks . The following ten tasks , Loop execution , So reuse threads , All threads are execution threads .

package com.hnucm;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {

public static void main(String[] args) {

// Create a cacheable thread pool 
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {

try {

// sleep It can be seen that the previous threads in the thread pool are used , No new thread created 
Thread.sleep(1000);
} catch (InterruptedException e) {

e.printStackTrace();
}
// Thread pool persistent tasks 
cachedThreadPool.execute(new Runnable() {

@Override
public void run() {

// Print cache thread information in progress 
System.out.println(Thread.currentThread().getName() + " Being implemented "); }
});
}
}
}

 Insert picture description here
2. newFixedThreadPool

Fixed number of worker threads , Multiple threads execute tasks concurrently , Therefore, the execution order cannot be determined . So here are three threads executing at the same time .

package com.hnucm;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {

public static void main(String[] args) {

// Create a fixed thread pool 
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {

fixedThreadPool.execute(new Runnable() {

@Override
public void run() {

try {

// Perform three tasks simultaneously 
// Three worker threads 
// Print cache thread information in progress 
System.out.println(Thread.currentThread().getName()+" Being executed ");
Thread.sleep(2000);
} catch (InterruptedException e) {

e.printStackTrace();
}
}
});
}
}
}

 Insert picture description here
3. newSingleThreadPool

Single threaded thread pool . Only one worker thread , Make sure that when you perform a task , Other tasks are waiting , Ensure that the tasks are executed in sequence .

package com.hnucm;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {

public static void main(String[] args) {

// Create a single worker thread pool 
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {

// Constant 
final int index = i;
singleThreadPool.execute(new Runnable() {

@Override
public void run() {

try {

// Results output in turn , Equivalent to performing tasks in sequence 
// The first i A mission 
System.out.println(Thread.currentThread().getName()+" Being executed , The printed value is :"+index);
Thread.sleep(1000);
} catch (InterruptedException e) {

e.printStackTrace();
}
}
});
}
}
}

 Insert picture description here
4. newScheduleThreadPool

Fixed length thread pool . For execution delay , timing , Periodic tasks .

  • Delay the
package com.hnucm;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {

public static void main(String[] args) {

// Create a fixed-length thread pool , Support regular and periodic task execution —— Delay the 
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
// Delay 1 Seconds to perform 
scheduledThreadPool.schedule(new Runnable() {

@Override
public void run() {

System.out.println(" Delay 5 Seconds to perform ");
}
}, 5, TimeUnit.SECONDS);
}
}
  • Delay + regular ( cycle ) perform
package com.hnucm;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {

public static void main(String[] args) {

// Create a fixed-length thread pool , Support regular and periodic task execution —— Delay the 
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
// Delay 1 Seconds to perform 
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {

@Override
public void run() {

System.out.println(" Delay 1 Seconds later 3 Once per second ");
}
}, 1, 3, TimeUnit.SECONDS);
}
}

 Insert picture description here

copyright:author[Tinker Bell is not lazy],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130909052368.html