【代码积累】join a thread

public class Test {
	public void test()
	{
		Thread subTask = new Thread(new showTask());
		
		/*使用了join,则主线程的打印必然在子线程之后*/
//		try {
//			subTask.start();
//			subTask.join();
//			System.out.println("This is main test thread!");
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		/*不使用join,则主线程的打印可能在子线程之前*/
		subTask.start();
		System.out.println("This is main test thread!");
	}
	
	private class showTask implements Runnable
	{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println("This is showTask!");
		}
	}
}

你可能感兴趣的:(【代码积累】join a thread)