首页 > 编程语言 >JAVA多线程——线程池

JAVA多线程——线程池

时间:2022-11-20 15:57:40浏览次数:42  
标签:正在 JAVA thread Executors 业务 线程 pool 多线程 办理

1. 固定线程池:Executors.newFixedThreadPool(5);


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

/**
 * @author huangdh
 * @version 1.0
 * @description:
 * @date 2022-10-30 22:31
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {

        // 固定线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程池
        ExecutorService threadPool3 = Executors.newCachedThreadPool();


        // 10个顾客请求
        try {
            for (int i = 0; i < 10; i++) {
                threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
                /*threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                /*threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
pool-1-thread-2 正在办理业务
pool-1-thread-3 正在办理业务
pool-1-thread-1 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-5 正在办理业务
pool-1-thread-3 正在办理业务
pool-1-thread-2 正在办理业务
pool-1-thread-4 正在办理业务
pool-1-thread-1 正在办理业务

2. 单例线程池:Executors.newSingleThreadExecutor();


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

/**
 * @author huangdh
 * @version 1.0
 * @description:
 * @date 2022-10-30 22:31
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {

        // 固定线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程池
        ExecutorService threadPool3 = Executors.newCachedThreadPool();


        // 10个顾客请求
        try {
            for (int i = 0; i < 10; i++) {
                /*threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
                /*threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务
pool-2-thread-1 正在办理业务

3. 可扩容线程池:Executors.newCachedThreadPool();


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

/**
 * @author huangdh
 * @version 1.0
 * @description:
 * @date 2022-10-30 22:31
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {

        // 固定线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5);

        // 一池一线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();

        // 可扩容线程池
        ExecutorService threadPool3 = Executors.newCachedThreadPool();


        // 10个顾客请求
        try {
            for (int i = 0; i < 10; i++) {
                /*threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                /*threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });*/
                threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 正在办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool3.shutdown();
        }
    }
}
pool-3-thread-1 正在办理业务
pool-3-thread-4 正在办理业务
pool-3-thread-3 正在办理业务
pool-3-thread-2 正在办理业务
pool-3-thread-5 正在办理业务
pool-3-thread-6 正在办理业务
pool-3-thread-7 正在办理业务
pool-3-thread-5 正在办理业务
pool-3-thread-2 正在办理业务
pool-3-thread-6 正在办理业务

 

标签:正在,JAVA,thread,Executors,业务,线程,pool,多线程,办理
From: https://www.cnblogs.com/huangdh/p/16908677.html

相关文章