join thread ()

http://blog.itpub.net/31555134/viewspace-2221319/

       He has been on the join () method is not very understanding, in A thread, B thread calls the join () method, and then inside actually wait () method, but in effect, A thread has been waiting for, to wait () method the call is not very understanding. (I hope that friends can give Q Thank you very much)

      Action join () method is to thread object x belongs normal execution run () method of the task, the current thread is blocked indefinitely z, x thread waits before proceeding back to destroy threads z code. Methods join the queue has a thread running role somewhat similar to the synchronous operating results.

The following is a join () method Source:

    /**
     * Waits for this thread to die.
     *
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final void join() throws InterruptedException {
        join(0);
    }
    / ** 
     * Waits AT MOST { @code millis} milliseconds for the this to the Thread 
     * Die. A timeout of { @code 0} means to the wait Forever. // 0 means wait forever not wait for 0 seconds means that only wait millis millis , over this time the stops waiting for 
     * 
     * <P> this uses Implementation of Loop A { @code this.wait Calls} 
     * {ON conditioned @code this.isAlive}. of as terminates the thread A // when the thread terminates, automatic call notifyAll () method 
     * { @code this.notifyAll method IS the invoked}. It Recommended that IS 
     * Applications Not use { @code the wait}, { @code  Notify}, or
     * { @code notifyAll} on {@code Thread} instances.
     *
     * @param  millis
     *         the time to wait in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

Of course, join () method on the start () method will be effective later.

Guess you like

Origin www.cnblogs.com/DDiamondd/p/11290833.html