标签:
B线程运行中调用a.join()后,意味着B线程加入到a线程尾部,即先执行a线程,后执行B线程。
package com.test;
public class B extends Thread {
public static void main(String[] args) throws Exception {
Thread t1 = new MyCommon();
Thread t2 = new Thread(new MyDaemon(t1));
t2.start();
t1.start(); } }
class MyCommon extends Thread { public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("线程1第" + i + "次执行!");
try {
Thread.sleep(7); }
catch (InterruptedException e) {
e.printStackTrace(); } }
} }
class MyDaemon implements Runnable { Thread t; public MyDaemon(Thread t) throws Exception{ this.t = t; }
public void run() { try { t.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } for (long i = 0; i < 10; i++) {
System.out.println("线程2第" + i + "次执行!"); try { Thread.sleep(7); } catch (
InterruptedException e) {
e.printStackTrace();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/xzybky/p/4321081.html