java例程练习(多线程[join()方法])

public class Test {
	public static void main(String[] args) {
		MyThread myThread = new MyThread("m1");
		myThread.start();   //产生分支,子线程开始执行
		
		try{
			myThread.join();//------等待合并myThread子线程,主线程才开始执行
		} catch(InterruptedException e) {}
		
		for(int i = 1; i <= 10; i++) {
			System.out.println("I am main thread");
		}
	}
}

class MyThread extends Thread {
	MyThread(String s) {//给线程起名字的构造方法
		super(s);
	}
	public void run() {
		for(int i = 0 ; i <= 10; i++) {
			System.out.println("I'm " + getName());
			try {
				sleep(1000);
			} catch(InterruptedException e) {
				return;
			}
		}
		
	}
}

posted on 2012-05-04 23:15  Yours风之恋  阅读(139)  评论(0编辑  收藏  举报