java中join的用法

看了代码一切就明白了。做下记录:

package com.bupt.test;

public class JoinTest {
	
	public static void main(String[] args) {
		Thread t = new Thread(new RunThread());
		t.start();
		try {
			t.join();  //等到t线程执行结束后,再继续执行
			System.out.println("main end");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class RunThread implements Runnable {

	@Override
	public void run() {
		System.out.println("Thread begin");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Thread end");
	}
	
}

package com.bupt.test;

public class JoinTest {
	
	public static void main(String[] args) {
		Thread t = new Thread(new RunThread());
		t.start();
		try {
			t.join(1000); //只等1秒,就继续执行。
			System.out.println("main end");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class RunThread implements Runnable {

	@Override
	public void run() {
		System.out.println("Thread begin");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Thread end");
	}
	
}

你可能感兴趣的:(java中join的用法)