这段代码创建了三个线程,分别命名为"换行"、"小白"和"小红"。这三个线程依次启动,并通过`join()`方法实现了线程间的等待和顺序执行。
具体来说,线程"换行"输出数字0到9,线程"小白"在"换行"线程执行完毕后开始输出数字0到9,线程"小红"在"小白"线程执行完毕后开始输出数字0到9。整个过程是有序的,每个线程依次执行并打印输出。
public class F {
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "-->" + i);
}
}
}, "换行");
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
thread1.join();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "-->" + i);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}, "小白");
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
try {
thread2.join();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "-->" + i);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}, "小红");
thread1.start();
thread2.start();
thread3.start();
}
}