zoukankan      html  css  js  c++  java
  • 线程中join()的用法

    Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行。

    public static void main(String[] args) throws InterruptedException
        {
            System.out.println("main start");
    
            Thread t1 = new Thread(new Worker("thread-1"));
            t1.start();
            t1.join();
            System.out.println("main end");
        }

    在上面的例子中,main线程要等到t1线程运行结束后,才会输出“main end”。如果不加t1.join(),main线程和t1线程是并行的。而加上t1.join(),程序就变成是顺序执行了。

    我们在用到join()的时候,通常都是main线程等到其他多个线程执行完毕后再继续执行。其他多个线程之间并不需要互相等待。

    下面这段代码并没有实现让其他线程并发执行,线程是顺序执行的。

    public static void main(String[] args) throws InterruptedException
        {
            System.out.println("main start");
    
            Thread t1 = new Thread(new Worker("thread-1"));
            Thread t2 = new Thread(new Worker("thread-2"));
            t1.start();
            //等待t1结束,这时候t2线程并未启动
            t1.join();
            
            //t1结束后,启动t2线程
            t2.start();
            //等待t2结束
            t2.join();
    
            System.out.println("main end");
        }

    为了让t1、t2线程并行,我们可以稍微改一下代码,下面给出完整的代码:

    public class JoinTest
    {
    
        public static void main(String[] args) throws InterruptedException
        {
            System.out.println("main start");
    
            Thread t1 = new Thread(new Worker("thread-1"));
            Thread t2 = new Thread(new Worker("thread-2"));
            
            t1.start();
            t2.start();
            
            t1.join();
            t2.join();
    
            System.out.println("main end");
        }
    }
    
    class Worker implements Runnable
    {
    
        private String name;
    
        public Worker(String name)
        {
            this.name = name;
        }
    
        @Override
        public void run()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    Thread.sleep(1l);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                System.out.println(name);
            }
        }
    
    }
  • 相关阅读:
    C语言---堆的实现
    python的matplotlib---雷达图
    python的matplotlib饼状图
    python的matplotlib折线图
    python的matplotlib散点图学习
    python的matplotlib散点图
    C语言---队列(链表实现)
    hadoop集群启动与关闭需要输入密码
    hadoop集群启动时需要输入密码
    C语言---堆栈(链表实现)
  • 原文地址:https://www.cnblogs.com/jfqiu/p/3264045.html
Copyright © 2011-2022 走看看