package com.atguigu.test;
public class Test1 {
public static void main(String[] args) {
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB(threadA);
ThreadC threadC = new ThreadC(threadB);
threadA.start();
threadB.start();
threadC.start();
}
}
class ThreadA extends Thread {
@Override
public void run() {
System.out.println("线程A");
}
}
class ThreadB extends Thread {
Thread threadA;
public ThreadB() {
// TODO Auto-generated constructor stub
}
public ThreadB(Thread threadA) {
this.threadA = threadA;
}
@Override
public void run() {
try {
threadA.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程B");
}
}
class ThreadC extends Thread {
Thread threadB;
public ThreadC(Thread threadB) {
this.threadB = threadB;
}
@Override
public void run() {
try {
threadB.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程C");
}
}
思路:
1.先在主线程开启三个线程。
2.分别向线程2传入线程1,线程3传入线程2,从而分别对它们实现控制。
这样也可以
public class Thread006 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("我是t1线程,i" + i);
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 30; i++) {
System.out.println("我是t2线程,i" + i);
}
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 30; i++) {
System.out.println("我是t3线程,i" + i);
}
}
});
thread.start();
thread2.start();
thread3.start();
}
}