Thread类的方法
public final void join()
作用:等待调用该方法的线程结束后才能执行
public final void join(long millis)
作用:等待该线程终止的最长时间为millis毫秒。
如果millis为0则意味着要一直等待下去。
Java
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(getName() + "正在执行" + i + "次");
}
}
}
public class JoinDemo {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
try {
mt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 1; i <= 15; i++) {
System.out.println("主线程运行第" + i + "次");
}
System.out.println("主线程运行结束!");
}
}
Java
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 300; i++) {
System.out.println(getName() + "正在执行" + i + "次");
}
}
}
public class JoinDemo {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
try {
mt.join(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 1; i <= 15; i++) {
System.out.println("主线程运行第" + i + "次");
}
System.out.println("主线程运行结束!");
}
}