• 三个线程依次顺序执行


    1.JOIN
    package com.gupao.forkjoin;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 功能:
     *
     * @author kangping
     * @date 2022-07-05 6:56 下午
     */
    public class Main {
    
        public static void main(String[] args) {
            final Thread t1 = new Thread(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " run 1");
                }
            }, "T1");
            final Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        // t2 线程阻塞 等待t1线程执行完
                        t1.join(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " run 2");
                }
            }, "T2");
            final Thread t3 = new Thread(new Runnable() {
                public void run() {
                    try {
                      // t3 线程阻塞 等待t2线程执行完
                        t2.join(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " run 3");
                }
            }, "T3");
            t1.start();
            t2.start();
            t3.start();
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    2.线程池

    newSingleThreadExecutor 里面只有1个线程,依次执行run方法

    package com.gupao.forkjoin;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 功能:
     *
     * @author kangping
     * @date 2022-07-05 6:56 下午
     */
    public class Main {
    
        public static void main(String[] args) {
            final Thread t1 = new Thread(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " run 1");
                }
            }, "T1");
            final Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        t1.join(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " run 2");
                }
            }, "T2");
            final Thread t3 = new Thread(new Runnable() {
                public void run() {
                    try {
                        t2.join(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " run 3");
                }
            }, "T3");
    
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.submit(t1);
            executorService.submit(t2);
            executorService.submit(t3);
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    离散数学之 一阶逻辑等值演算与推理
    luogu P5560 [Celeste-B]Golden Feather
    美芯片禁令再次扩大,波及英伟达、AMD以及intel等科技公司 | 百能云芯
    双向ip语音对讲音柱
    学生信息管理模块设计与开发
    建站百科:常见的FTP软件有哪些?
    U盘目录穿越获取车机SHELL - 分析与复现
    GBase 8a事务控制
    LLVM系列第二十八章:写一个JIT Hello World
    Hostlink通讯协议解析【串行 C-Mode和Fins】
  • 原文地址:https://blog.csdn.net/weixin_40980639/article/details/125626076