• Java线程任务 创建一个单独的任务线程并提交到线程池中执行 案例


    准备

    1. 当前文件中声明一个线程池并给定线程数量
    public class App 
    {
        private static final ExecutorService executorService = Executors.newFixedThreadPool(20);
        ..................
        ..................
        ..................
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 创建单独的任务线程 实现业务
    public class BusinessThread implements Runnable{
    
        private int numbers;
    
        // 业务注入 可以通过 applicationContextAware 相关的类获取spring 中的bean做其他服务
    
        public BusinessThread(int number ) {
            this.numbers = number;
        }
    
        @Override
        public void run() {
            System.out.println("业务处理");
            numbers *= 2;
            System.out.println("numbers = " + numbers);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 提交任务到线程池中
        public static void main(String[] args){
            executorService.execute(new BusinessThread(6));
            executorService.shutdown();
            try {
                executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    当前的测试结果:

    业务处理
    numbers = 12
    
    • 1
    • 2

    在 main 中单独测试时如果我没有调用 shutdown 方法时。即

        public static void main(String[] args){
            executorService.execute(new BusinessThread(6));
        }
    
    • 1
    • 2
    • 3

    此时结果依旧一致 但是 main 方法没有停止运行,依旧处于运行中,添加 shutdown() 后其实任务已经停止

        public static void main(String[] args){
            executorService.execute(new BusinessThread(6));
            executorService.shutdown();
        }
    
    • 1
    • 2
    • 3
    • 4

    测试结果依旧一样。
    可见 awaitTermination() 作用与JDK 接口描述的一致 其实并不是必须的。

    awaitTermination() 作用即: 阻塞,直到所有任务在 shutdown 请求后完成执行,或发生超时,或当前线程中断,以先发生的为准。
    即在发出 shutdown 请求后会一直阻塞到所有任务完成或者当前线程出现异常或者超过指定的超时时间。

    尝试 将execute 换为 submit

        public static void main(String[] args){
    
            executorService.submit(new BusinessThread(6));
            executorService.shutdown();
            try {
                executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    测试后发现依旧可以照常执行,执行结果照旧:

    业务处理
    numbers = 12
    
    • 1
    • 2

    submit()
    提交一个可执行任务到线程中
    execute()
    在未来的某个时间执行给定的命令或任务,可能在一个新的线程中执行,也可能在一个线程池中执行,或在调用的线程中执行,这由 Executor 的实现决定。

    使用 thread 的方式实现任务线程

    额外定义任务如下 使用他hread 的方式

    public class Business2Thread extends Thread{
    
        private int numbers;
    
        public Business2Thread(int numbers) {
            this.numbers = numbers;
        }
    
        @Override
        public void run() {
            System.out.println("业务2处理");
            numbers <<= 1;
            System.out.println("numbers = " + numbers);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    main 中实验如下:

        public static void main(String[] args){
    
            executorService.submit(new Business2Thread(6));
    
            executorService.execute(new BusinessThread(6));
            executorService.shutdown();
            try {
                executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这里不论使用 submit 还是 execute 都可以,测试结果如下:

    业务2处理
    numbers = 12
    业务处理
    numbers = 12
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    关于组合数(二项系数)的一个递推公式
    【Unity3D】角色控制器(CharacterController)
    Java 8实战(四)- Lambda类型推断与方法引用
    Java之HashMap经典算法-红黑树(插入节点平衡调整,左旋转,右旋转)
    小红书发布2022年美妆用户洞察报告,近八成用户选它
    「互动有礼,感谢有你」参与互动就有机会获赠 Navicat Premium 16
    第07章 连接Hadoop集群
    MySQL基础与库的基本操作
    鉴源实验室 | 自动驾驶传感器攻击研究
    线程和进程的优缺点
  • 原文地址:https://blog.csdn.net/weixin_44131922/article/details/126483295