Threadのjoinの実現にちょっと迷うところ


	
	package test;	

	/**	
	 * Created by IntelliJ IDEA.	
	 * Date: 11/06/28
	 * Time: 10:26
	 */
	public class IsThreadPool {
	    public static void main(String[] args) {
	        System.out.println("begin >>> " + Thread.currentThread().getName() + "  isAlive: " + Thread.currentThread().isAlive());
	        ChildThread child1 = new ChildThread();
	        child1.start();
	        try {
	            child1.joinExtend(0);
	//            child1.join(0);
	        } catch (InterruptedException e) {
	            e.printStackTrace();
	        }
	        System.out.println("end >>> " + Thread.currentThread().getName() + "  isAlive: " + Thread.currentThread().isAlive());
	    }

	}

	class ChildThread extends Thread {
	    public void run() {
	        int i = 4;
	        while (--i >= 0) {
	            try {
	                Thread.sleep(2000);
	            } catch (InterruptedException e) {
	                e.printStackTrace();
	            }
	            System.out.println("==============================================================");
	            System.out.println(Thread.currentThread().getName() + "  isAlive: " + Thread.currentThread().isAlive());
	            ThreadGroup tg = Thread.currentThread().getThreadGroup();
	            System.out.println(tg.toString());
	            Thread[] threads = new Thread[tg.activeCount()];
	            tg.enumerate(threads);
	            for (Thread t : threads) {
	                System.out.println("    ThreadGroup_child: " + t.getName() + "   state: " + t.getState());
	            }
	            tg = null;

	        }


	    }

	    /**
	     *  Thread join , ,
	     * isAlive(): ( ) 。 , 。
	     * wait:  notify()   notifyAll()  , , 
	     * @param millis
	     * @throws InterruptedException
	     */
	    public final synchronized void joinExtend(long millis)
	            throws InterruptedException {

	        long base = System.currentTimeMillis();
	        long now = 0;
	        boolean isAlive = this.isAlive();
	        while (isAlive = this.isAlive()) {
	            System.out.println("joinExtend_currentThread--->  " + Thread.currentThread().getName());
	            System.out.println("this ---> " + this.getName() + "   alive---> " + isAlive + "  currentThread---> " + Thread.currentThread().getName());
	            this.wait(0);
	        }

	    }
	}

	

結果:
Connected to the target VM, address: '127.0.0.1:1759', transport: 'socket'
begin >>> main  isAlive: true
joinExtend_currentThread--->  main
this ---> Thread-0   alive---> true  currentThread---> main
==============================================================
Thread-0  isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
    ThreadGroup_child: main   state: WAITING
    ThreadGroup_child: Thread-0   state: RUNNABLE
==============================================================
Thread-0  isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
    ThreadGroup_child: main   state: WAITING
    ThreadGroup_child: Thread-0   state: RUNNABLE
==============================================================
Thread-0  isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
    ThreadGroup_child: main   state: WAITING
    ThreadGroup_child: Thread-0   state: RUNNABLE
==============================================================
Thread-0  isAlive: true
java.lang.ThreadGroup[name=main,maxpri=10]
    ThreadGroup_child: main   state: WAITING
    ThreadGroup_child: Thread-0   state: RUNNABLE
end >>> main  isAlive: true
Disconnected from the target VM, address: '127.0.0.1:1759', transport: 'socket'
Process finished with exit code 0