• java 多线程,T1 T2 T3 顺序执行


    一、程序设计

    1.抽象公共类PublicThread,具有先前线程属性previousThread、父类为Thread

    2.在PublicThread的run()方法中判断previousThread属性是否为null,如果不为null,执行previousThread的join()方法

    3.定义PublicThread的构造方法:1个空参 1个以previousThread为构造参数

    3.创建测试类ThreadOrder,创建三个PublicThread对象,进行测试

    二、代码实现

    PublicThread

    package org.zheng.thread;
    
    public class PublicThread extends Thread{
        private Thread previousThread = null;
        
        public PublicThread() {
            super();
        }
        
        public PublicThread(Thread previousThread) {
            super();
            this.previousThread = previousThread;
        }
        @Override
        public void run() {
            if(previousThread!=null){
                try {
                    previousThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName());
        }
    }

    测试类ThreadTest

    package org.zheng.thread;
    
    public class ThreadTest {
    
        public static void main(String[] args) {
            Thread t1 = new PublicThread();
            t1.setName("T1");
            Thread t2 =  new PublicThread(t1);
            t2.setName("T2");
            Thread t3 = new PublicThread(t2);
            t3.setName("T3");
            
            t1.start();
            t2.start();
            t3.start();
        }
    
    }
  • 相关阅读:
    c语言中逆向显示数值
    c语言中无限循环语句
    css静态网页css代码
    css静态网页css代码
    考试html代码
    考试css代码
    考试html框架代码
    css静态网页代码
    5个CSS书写技巧
    5个CSS书写技巧
  • 原文地址:https://www.cnblogs.com/zhengwenqiang/p/7708360.html
Copyright © 2020-2023  润新知