美文网首页编程路上
head first Thread.join()

head first Thread.join()

作者: a9b430bb6daa | 来源:发表于2017-08-24 18:24 被阅读24次

不使用Thread.join() 测试线程

先上代码:

/**
 * Created by Zero on 2017/8/23.
 */
public class TestJoin implements Runnable {
    public static int a = 0;

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            a = a + 1;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestJoin j = new TestJoin();
        Thread thread = new Thread(j);
        thread.start();
        System.out.println(a);
    }
}

以上示例会输出5吗?可能性不大,有可能永远输出为0,之前在线程池原理的那篇就提到过,线程的启动和销毁都需要时间,此处因为thread还没启动好,或者正在为它分配资源准备运行,就已经执行完输出了。

怎样才能确保每次都能输出5呢?现在有请我们的主角join方法闪亮登场,代码如下:

/**
 * Created by apple on 2017/8/23.
 */
public class TestJoin implements Runnable {
    public static int a = 0;

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            a = a + 1;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestJoin j = new TestJoin();
        Thread thread = new Thread(j);
        thread.start();
        /**
         * 测试join方法的作用,与下面的threadAgain线程作对比。
         */
        thread.join();
        System.out.println(a);
        a = 0;
        Thread threadAgain = new Thread(j);
        threadAgain.start();
        System.out.println(a);
    }
}

输出的结果将是5和0。

Thread.join()作用

Thread.join(),之前看资料的时候,有些人说可以理解成“将两个线程合并成一个线程”,我是觉得这样说是很不科学的,虽然这样通俗易懂,但这确实是两个不同的线程,只是在调用Thread.join()后,会先执行完Thread线程后再去执行当前线程,即上述的在主线程中执行到thread.join();后,先去执行thread,直到thread执行完后再去执行主线程。

测试Thread.join(long millis)

/**
 * Created by apple on 2017/8/23.
 */
public class TestJoin implements Runnable {
    public static int a = 0;

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            a = a + 1;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestJoin j = new TestJoin();
        Thread thread = new Thread(j);
        thread.start();
        /**
         * 测试join方法的作用
         */
        thread.join(3000);
        System.out.println("thread线程结果为:"+a);
        a = 0;
        Thread threadAgain = new Thread(j);
        threadAgain.start();
        System.out.println("threadAgain线程结果为:"+a);
    }
}

输出:

thread线程结果为:3
threadAgain线程结果为:0

先上一段源码再来分析:

/**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *
     * <p> This implementation uses a loop of {@code this.wait} calls
     * conditioned on {@code this.isAlive}. As a thread terminates the
     * {@code this.notifyAll} method is invoked. It is recommended that
     * applications not use {@code 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;
            }
        }
    }

源码爸爸说了,孩子,我给你millis这么长的时间,能不能完成任务那是你的事情了,能提前完成,咱就提前走下去,不能完成,过期不候,自己看着办吧。

默认情况下,Thread.join()即Thread.join(0),当为0的时候,那才叫真爱呢,线程会一直等下去,知道执行结束为止,才会继续朝下执行。

isAlive():用来测试线程是否处于活动状态,相当于 run 是否还在执行。

相关文章

  • head first Thread.join()

    不使用Thread.join() 测试线程 先上代码: 以上示例会输出5吗?可能性不大,有可能永远输出为0,之前在...

  • 2018-12-11

    head first html css word书籍 http权威指南 head first设计模式

  • HTML+CSS

    (待续。。。) 1.Head First HTML与CSS(第2版) [Head First HTML and C...

  • Head First C 学习之K&R C 、ANSI

    @(C语言)[学习笔记, Head First C, C语言]起于Head First C 第2页 下, 书中简介...

  • 设计模式--策略模式

    ps:本文主要来源于Head First 设计模式(抄Head First的),如有不懂请购买原书观看。 策略模式...

  • Head First Java(一)基本概念

    从今天开始读《Head First Java》一书,并开设了同名专题 Head First Java。计划在 1 ...

  • Head First Python

    下载地址:Head First Python一本免费的电子书,胜过一杯付费的咖啡

  • Head First Java

    变量类型 变量类型有两种:一种是清凉的 primitive 主数据类型,一种是香辣的对象引用。变量必须拥有类型,另...

  • Head First:Redux

    一.什么是Redux? Redux是最近很火的一个前端数据流框架,之前接触过Flux应该很熟悉了。Redux可以看...

  • Head First Programmer

    目录 写在最前面

网友评论

    本文标题:head first Thread.join()

    本文链接:https://www.haomeiwen.com/subject/mrgedxtx.html