ITPub博客

首页 > IT职业 > IT职场 > 多线程之join方法

多线程之join方法

IT职场 作者:misuland 时间:2018-11-23 10:33:29 0 删除 编辑


   1.jpg

  在很多情况下,我们都是通过主线程创建并启动子线程的,如果子线程中需要耗费大量的时间计算的话,主线程往往会比子线程先结束,这个时候就会导致有时候主线程想获取子线程计算之后的结果,但是却获取不到。这个时候,我们就可以通过join方法来解决这个问题。

  

  二join方法的作用:

  

  join方法的作用是使所属的线程对象x正常执行run()方法中的任务,而使当前线程z进行无限期的阻塞,等待线程x销毁后再继续执行线程z后面的代码。方法join具有使线程排队运行的作用,有些类似同步的运行效果。

  

  下面看一个例子:

  

public class MyThread extends Thread{

    @Override

    public void run() {

        int sencondValue = (int)(Math.random()*1000);

        System.out.println(sencondValue);

        try {

            Thread.sleep(sencondValue);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

}



public class Run {

    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();

        myThread.start();

        myThread.join();

        System.out.println("当对象myThread执行完毕后再执行");

    }

}

 

  

  运行结果:

  

  join的意思是使得放弃当前线程的执行,并返回对应的线程,例如上面代码的意思就是:程序在main线程中调用myThread线程的join方法,则main线程放弃cpu控制权,并返回t1线程继续执行直到线程t1执行完毕。所以结果是t1线程执行完后,才到主线程执行,相当于在main线程中同步t1线程,t1执行完了,main线程才有执行的机会 此外,join方法和interrupt方法相遇的时候会抛出异常。

  

  此外,还有join(long millis)方法,指定要等待多长时间。

  

  三。join的实现原理:

  

  查看join方法源码:

  

/**

     * 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);

    }


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;

            }

        }

    }

  

  可以看出,join方法实际上是通过调用wait方法, 来实现同步的效果的。例如,A线程中调用了B线程的join方法,则相当于A线程调用了B线程的wait方法,在调用了B线程的wait方法后,A线程就会进入阻塞状态,因为它相当于放弃了CPU的使用权。需要注意的是,jdk规定,join(0)的意思不是A线程等待B线程0秒,而是A线程等待B线程无限时间,直到B线程执行完毕,即join(0)等价于join()。

  

  四。join方法和synchronized、sleep方法的区别:

  

  1.从上面的源码我们可以看出,join(long millis)是通过在内部使用wait(long millis)方法来实现的,所有它其实是具有释放锁的特点的,在执行完;而sleep(long millis)是不释放锁的,也就是如果有Synchronized同步块,其他线程仍然不能访问共享数据。注意该方法要捕获异常。

  

  2.join在内部使用wait()方法进行等待,而synchronized关键字使用的是“对象监控器”原理做同步

  

  这里小编给大家推荐一个开发在线交易平台——米鼠网,米鼠网是一个创新的复杂性项目在线交易平台,其服务的种类包括了政府采购、软件开发,定制开发、人才外包、等。项目进度可远程监控进度确保项目质量米鼠网对于买家而言,提供了强大的供应商资源,并大幅降低了成本;对乙方而言,则提供了无限的商业机会,双方互惠互利,并有保证金制度约束双方交易流程



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31555134/viewspace-2221319/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
米鼠网是一个创新的软件开发在线交易平台。它能帮助软件买家更好的节省采购成本,监控项目进度确保项目质量;同时它还能帮助软件卖家节省运营成本,解决项目来源等问题。

注册时间:2018-09-17

  • 博文量
    42
  • 访问量
    27452