必威体育Betway必威体育官网
当前位置:首页 > IT技术

thread.join,理解与实例(一)

时间:2019-10-17 20:42:19来源:IT技术作者:seo实验室小编阅读:60次「手机版」
 

thread.join

java线程编程join的作用是等待线程结束,这个作用可以产生很多特定的场景。 

1)A线程中调用B线程的join方法,那么A线程需要等待B线程执行完成后才能完成 

2)主线程中依次调用A线程的join方法,B线程的join方法,可以保证A,B线程顺序执行;

是主线程进入等待状态,子线程在运行,子线程运行完成后会通知主线程继续运行,或者join也可以设置主线程的等待时间,当主线程等待超时时,即使子线程没有运行完,主线程也会开始继续执行,

实例1:

public class jointest1 {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new MyRunnable(), "t1");
        t1.start();

        new JoinTest1().listAllThread();

//        t1.join();//等价于t1.join(0);一直等待到线程t1结束,主线程才继续运行
//        t1.join(1000);//线程t1等待1s后,会超时,此时t1没结束,主线程此时会继续运行
        t1.join(6000);//线程t1的等待时间大于运行时间,t1运行结束后,主线程才继续运行,不必等待6s
//        t1.join(3000);//两个时间一致,线程t1运行结束后,主线程才继续运行

        new JoinTest1().listAllThread();

        System.out.println(new Date() + "主线程");

    }


    /**
     * 显示该程序的所有线程
     */
    public void listAllThread() {
        System.out.println(new Date() + " listAllThread...");
        ThreadGroup currentGroup =
                Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        for (int i = 0; i < noThreads; i++)
            System.out.println("线程号:" + i + " = " + lstThreads[i].getName());
    }
}


class MyRunnable implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);//模拟使线程运行3s
        } catch (InterruptedException e) {
            e.printstacktrace();
        }
    }
}

输出:

t1.join();

Sun Sep 30 11:22:01 CST 2018 listAllThread...

线程号:0 = main

线程号:1 = t1

Sun Sep 30 11:22:04 CST 2018 listAllThread...

线程号:0 = main

Sun Sep 30 11:22:04 CST 2018主线程

//线程t1已结束

t1.join(1000);

Sun Sep 30 11:22:42 CST 2018 listAllThread...

线程号:0 = main

线程号:1 = t1

Sun Sep 30 11:22:43 CST 2018 listAllThread...

线程号:0 = main

线程号:1 = t1 //线程t1仍然存活

Sun Sep 30 11:22:43 CST 2018主线程

t1.join(6000);

Sun Sep 30 11:25:18 CST 2018 listAllThread...

线程号:0 = main

线程号:1 = t1

Sun Sep 30 11:25:21 CST 2018 listAllThread...

线程号:0 = main

Sun Sep 30 11:25:21 CST 2018主线程

//线程t1已结束

t1.join(3000);

Sun Sep 30 11:36:43 CST 2018 listAllThread...

线程号:0 = main

线程号:1 = t1

Sun Sep 30 11:36:46 CST 2018 listAllThread...

线程号:0 = main

Sun Sep 30 11:36:46 CST 2018主线程

//线程t1已结束

join源码,java1.8:

/**
     * 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;
            }
        }
    }

可见另一篇博客

thread.join,理解与实例(二)》https://blog.csdn.net/u010002184/article/details/82910229

相关阅读

Jointjs初认识

最近在研究Jointjs,这个做流程图、工作表、图标真的是很强大: 1、新建画板、画布 joint.setTheme('modern'); //

【MySQL】跨库join

目标:数据库A中的表可以join数据库B中的表。 环境:Windows系统,免安装版mysql-5.7.22。 需求:数据库中表很多,将表按业务划分到不同的

Java Thread之Sleep()使用方法总结

一、API简介 Thread.sleep()是Thread类的一个静态方法,使当前线程休眠,进入阻塞状态(暂停执行),如果线程在睡眠状态被中断,将会抛出Ite

HandlerThread(详细例子)

HandlerThread是什么 官方解释 Handy class for starting a new thread that has a looper. The looper can then be used to cre

java中Executor框架之ThreadPoolExcutor和ScheduledTh

转载自: https://blog.csdn.net/javazejian/article/details/50890554 java多线程-概念&创建启动&中断&守护线程&

分享到:

栏目导航

推荐阅读

热门阅读