wait不一定需要notify

通过join源码猜想线程的实例在运行结束后会自动清空wait set队列,唤醒所有线程。




package com.test;

public class test5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread thread = new MyThread();
		
		thread.start();
		
		thread.myJoin();
		System.out.println("主线程结束");

	}

	
	
	
	static class MyThread extends Thread{
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				System.out.println("MyThread 睡眠前");
				Thread.sleep(5000);
				System.out.println("MyThread 睡眠后");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		public void myJoin(){
			
			synchronized(this){
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}


你可能感兴趣的:(wait不一定需要notify)