• 三个线程T1,T2,T3.保证顺序执行的三种方法


    第一种方式:顺序在线程中创建实例(最容易想到的办法)。

    public class TestTwo {
            static TestTwo t=new TestTwo();
            class T1 extends Thread{
                @Override
                public void run() {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //T1线程中要处理的东西
                    System.out.println("T1线程执行")
                }
            }
    
            class T2 extends Thread{
                @Override
                public void run() {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //T2线程中要处理的东西
                    System.out.println("T2线程执行");
                    t.new T1().start();
                }
            }
    
            class T3 extends Thread{
                @Override
                public void run() {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //T3线程中要处理的东西
                    System.out.println("T3线程执行");
                    t.new T2().start();
                }
            }
            
            public static void main(String[] args) {
                t.new T3().start();
           //打印结果如下:
                 //T3线程执行
            //T2线程执行
                  //T1线程执行
            }
    }
    
    • 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
    • 50
    • 51

    第二种方式:

    • 看到有人说运用单个线程池(SingleThreadExecutor)来实现,确切的说这里不太符合,从打印结果看出,其实我们是在一个线程里,执行了三个任务。
    Thread t1 = new Thread(new Runnable() {
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " run 1");
                    }
                }, "T1");
                 Thread t2 = new Thread(new Runnable() {
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " run 2");
                    }
                }, "T2");
                 Thread t3 = new Thread(new Runnable() {
                    public void run() {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " run 3");
                    }
                }, "T3");
    
                //三个线程顺序执行 第一种方案,单个线程池 顺序放入执行队列中
                ExecutorService executor = Executors.newSingleThreadExecutor();
    
                executor.submit(t3);
                executor.submit(t2);
                executor.submit(t1);
                executor.shutdown();
                //输出结果如下:
    //            pool-1-thread-1 run 3
    //            pool-1-thread-1 run 2
    //            pool-1-thread-1 run 1
    
    • 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

    第三种:运用线程的 join 方法来实现

    public class JoinTest2 {
    	// 1.现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
    	public static void main(String[] args) {
    		final Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				System.out.println("t1");
    			}
    		});
    	final Thread t2 = new Thread(new Runnable() {
    	@Override
    	public void run() {
    		try {
    			// 引用t1线程,等待t1线程执行完
    			t1.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    			System.out.println("t2");
    		}
    	});
    	Thread t3 = new Thread(new Runnable() {
    	@Override
    	public void run() {
    		try {
    			// 引用t2线程,等待t2线程执行完
    			t2.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    			System.out.println("t3");
    		}
    	});
    	t3.start();//这里三个线程的启动顺序可以任意,大家可以试下!
    	t2.start();
    	t1.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

    详细链接http://t.zoukankan.com/liran123-p-9313830.html

  • 相关阅读:
    开发指导—利用CSS动画实现HarmonyOS动效(一)
    skaffold提升K8s开发效率
    OpenCV C++ 图像 批处理 (批量调整尺寸、批量重命名)
    谱定理等周边定理
    Stable Diffusion教程
    代码随想录算法训练营第60天|84.柱状图中最大的矩形
    轻量应用服务器vs云服务器:区别在哪?
    一文读懂 TsFile
    ​ 生产者消费者问题(条件变量 & 互斥锁)
    MacBook通过XGP玩女神异闻录5皇家版不踩坑指南
  • 原文地址:https://blog.csdn.net/weixin_45035342/article/details/126744312