zoukankan      html  css  js  c++  java
  • Java线程中的join使用实例

    JDK中解释为 Waits for this thread to die. 等待本线程结束后,下一个线程才可以运行。

    实例要求:

    现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行

    实现代码:

    package com.st.lesson02;
    
    public class Test01 {
        //1.现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
        public static void main(String[] args) throws InterruptedException {
            Thread th1 = new Thread01();
            Thread th2 = new Thread02();
            Thread th3 = new Thread03();
            th1.start();
            th1.join();
            System.out.println("Thread01运行结束。。。");
            th2.start();
            th2.join();
            System.out.println("Thread02运行结束。。。");
            th3.start();
            th3.join();
            System.out.println("Thread03运行结束。。。");
            System.out.println("------主函数-------");
        }
    }
    class Thread01 extends Thread{
        
        public void run() {
            System.out.println("Thread01...running...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    class Thread02 extends Thread{
        public void run() {
            System.out.println("Thread02...running...");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }
    class Thread03 extends Thread{
        public void run() {
            System.out.println("Thread03...running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }

    运行效果图:

  • 相关阅读:
    安装SQL Server驱动到Maven仓库[转]
    Nuget 摘录
    删除除了Src属性以后的全部属性
    执行NET 命令无法使用超过20个字符的组名或用户名
    在EntityFramework中使用 nock的方法。
    两代码的区别
    SQLServer 执行计划
    win7电脑的账户被禁用了怎么办
    win10防火墙损坏如何修复
    win10摄像头在哪打开?
  • 原文地址:https://www.cnblogs.com/xiaotao726/p/6493099.html
Copyright © 2011-2022 走看看