让子线程执行完后,主线程再执行

public class Test {
	public static void main(String[] args) {
		System.out.println("主线程开始");
		AThread thread = new AThread();
		thread.start();
		try {
			thread.join();   //关键代码,子线程执行完后,再执行主线程
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("---------主-------主--------主---主线程结束---------------主--------主-----主---");
	}
	
	public static class AThread extends Thread {
		public void run() {
			System.out.println("子线程执行");
			int i = 10;
			while(i > 0) {
				i--;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
			}
			System.out.println("----------------子-------子-----子线程结束------------子------子-------------");
		}
	}
}

  

posted @ 2021-12-24 10:37  信铁寒胜  阅读(285)  评论(0编辑  收藏  举报