当前位置:网站首页>Three methods of thread pool

Three methods of thread pool

2022-06-22 06:18:00 Kuxiaoya

Three ways to pool threads

Executors.newSingleThreadExecutor(); Single thread

Executors.newFixedThreadPool(5); Fixed number of threads

Executors.newCachedThreadPool(); Buffer pool , Scalable

Executors.newSingleThreadExecutor(); Single thread

package pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** * Executors  Tool class 、3 The big way  */

public class Demo01 {
    
    public static void main(String[] args) {
    
        ExecutorService threadPool = Executors.newSingleThreadExecutor();// Single thread 

        try {
    
        for (int i = 0; i < 10; i++) {
    
            // After using the thread pool , Use thread pool to create threads 
            threadPool.execute(()->{
    
                System.out.println(Thread.currentThread().getName()+"OK!");
            });
        }
        } catch (Exception e) {
    
            e.printStackTrace();
        } finally {
    
            // Out of thread pool , Program end , Close thread pool 
            threadPool.shutdown();
        }
    }
}

Executors.newFixedThreadPool(5); Fixed number of threads

package pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** * Executors  Tool class 、3 The big way  */

public class Demo01 {
    
    public static void main(String[] args) {
    
        ExecutorService threadPool = Executors.newFixedThreadPool(5); // Create a fixed thread pool size 
    
        try {
    
        for (int i = 0; i < 10; i++) {
    
            // After using the thread pool , Use thread pool to create threads 
            threadPool.execute(()->{
    
                System.out.println(Thread.currentThread().getName()+" OK!");
            });
        }
        } catch (Exception e) {
    
            e.printStackTrace();
        } finally {
    
            // Out of thread pool , Program end , Close thread pool 
            threadPool.shutdown();
        }
    }
}

Executors.newCachedThreadPool(); Buffer pool , Scalable

1package pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** * Executors  Tool class 、3 The big way  */

public class Demo01 {
    
    public static void main(String[] args) {
    

        ExecutorService threadPool = Executors.newCachedThreadPool();// scalable , If you are strong, you will be strong , If you are weak, you will be weak 

        try {
    
        for (int i = 0; i < 100; i++) {
    
            // After using the thread pool , Use thread pool to create threads 
            threadPool.execute(()->{
    
                System.out.println(Thread.currentThread().getName()+" OK!");
            });
        }
        } catch (Exception e) {
    
            e.printStackTrace();
        } finally {
    
            // Out of thread pool , Program end , Close thread pool 
            threadPool.shutdown();
        }
    }
}
原网站

版权声明
本文为[Kuxiaoya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220609550465.html