一、程序设计
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(); } }