zoukankan      html  css  js  c++  java
  • 线程join

     
    class ThreadB extends Thread
    {
        private String ID="0";
        private int time=0;
        public ThreadB(String ID,int time)
        {
            this.ID=ID;
            this.time=time;
        }
        public void run()
        {
            System.out.println("线程A"+ID+"开始了");
            int i=1;
            try
            {
                while(i<=time)
                {
                    System.out.println("线程A"+ID+"运行的 次数"+ i++);
                    Thread.sleep(200);
                }
            } catch (Exception e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }
            System.out.println("线程A"+ID+"结束");
        }
     
    }
     
     
    public class JoinThrea
    {
        public static void main(String []args)
        {
            Thread thread1=new ThreadB("1",3);
            Thread thread2=new ThreadB("2",10);
            Thread thread3=new ThreadB("3",5);
            Thread thread4=new ThreadB("4",15);
            thread1.start();
     
            try
            {
                thread1.join();//只有当此线程结束 后面的线程才会开始
            } catch (Exception e)
            {
                // TODO: handle exception
            }
            thread2.start();
            thread3.start();
            thread4.start();
            try
            {
                thread1.join();
                thread2.join();
                thread3.join();
                thread4.join();
            } catch (InterruptedException  e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }
     
            System.out.println("全部结束");//只有当以上所有线程结束后才会 运行
        }
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    POJ 1988 Cube Stacking(带权并查集)
    POJ 1414 Life Line(搜索)
    POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
    二叉树前序遍历+中序遍历->后序遍历
    POJ 1061 青蛙的约会(扩展欧几里得)
    XDOJ 1020 ACMer去刷题吧(基础dp)
    POJ 2823 Sliding Window (线段树区间查询)
    线段树学习(一)
    UVA 294 Divisors( 因子分解)
    YYN图论学习路线(推荐)
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710170.html
Copyright © 2011-2022 走看看