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("全部结束");//只有当以上所有线程结束后才会 运行
        }
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    【EntityFramework系列教程十,翻译】ASP.NET MVC程序中的一些高级应用
    对不含数据源的DataGridView实现自定义排序
    poj 1584 A Round Peg in a Ground Hole(叉积判断凸多边形)
    大整数运算
    poj 1408 Fishnet(计算几何)
    poj 1201 Intervals(第一道差分约束题)
    poj 2983 Is the Information Reliable?(差分约束)
    poj 2187 Beauty Contest(凸包+旋转卡壳)
    poj 2031 Building a Space Station(prim)
    poj 3007 Organize Your Train part II
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710170.html
Copyright © 2011-2022 走看看