class MyThread2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++){
System.out.println(Thread.currentThread().getName() + ": " +i);
}
}
}
public class Test2 {
/**
* t1, t2, 主线程 三个线程顺序执行
*/
public static void main(String[] args) throws InterruptedException {
MyThread2 myThread2 = new MyThread2();
Thread t1 = new Thread(myThread2, "线程A");
Thread t2 = new Thread(myThread2, "线程B");
t1.start();
t1.join();
t2.start();
t2.join();
System.out.println("主线程...");
}
/**
* t1, t2 并行执行, 等 t1, t2 都执行完毕再执行主线程
*/
public void test2() throws InterruptedException {
MyThread2 myThread2 = new MyThread2();
Thread t1 = new Thread(myThread2, "线程A");
Thread t2 = new Thread(myThread2, "线程B");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("主线程...");
}
}