Posted on 2009-06-02 20:37
啥都写点 阅读(166)
评论(0) 编辑 收藏 所属分类:
J2SE
关键技术:
- 调用线程A的join方法表示当前线程必须等待线程A运行完后才能够继续运行。
- 可以为join提供参数,指定当前线程的最长等待时间(毫秒数)。
package book.thread;
/**
* 线程的结合。
* 当一个线程需要等待另一个线程结束时,叫做线程的结合。
*/
public class JoinThread {
/** 自定义线程类 */
static class ThreadA extends Thread{
//线程的ID
private int ID = 0;
//线程运行时循环的次数
private int whileTimes = 0;
public ThreadA(int id, int times){
this.ID = id;
this.whileTimes = times;
}
public void run(){
System.out.println("ThreadA" + this.ID + " begin!");
int i=0;
try {
//连续循环whileTimes次
while (i < this.whileTimes){
System.out.println("ThreadA-" + this.ID + ": " + i++);
//sleep方法将当前线程休眠。
Thread.sleep(200);
}
} catch (InterruptedException e) {
}
System.out.println("ThreadA" + this.ID + " end!");
}
}
public static void main(String[] args) {
//新建4个线程对象
Thread thread1 = new ThreadA(1, 3);
Thread thread2 = new ThreadA(2, 2);
Thread thread3 = new ThreadA(3, 2);
Thread thread4 = new ThreadA(4, 4);
//启动所有线程
System.out.println("Main method begin. To start 4 threads!");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
//等待所有线程运行结束
try {
thread1.join();
thread2.join();
thread3.join();
thread4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//此时所有线程都运行结束
System.out.println("Main method end! All 4 threads are ended");
}
}
-- 学海无涯