• 16,join(等待该线程终止)


    join 用于临时加入线程运行

     package songyan;
    /*
     * 主线程读到t1.join();
     * 主线程变为冻结状态
     * 放弃执行权
     * t1 运行结束
     * 主线程恢复运行状态
     * t2.start()
     * main线程,t2一起抢夺执行权
     * */ 
     
     /*
      * t1.start();
      * t2.start();
      * t1.join();
      * 
      * t1,t2 开启
      * main线程执行到t1.join()
      * main 冻结
      * t1,t2 抢夺执行权
      * t1运行完后main重新获得执行权
      * main 获得执行权的时机与t2无关
      * */
     class Demo implements Runnable
     {
         public void run(){
             for(int i=0;i<600;i++)
             {
                 System.out.println(Thread.currentThread().getName()+"run ***"+i);
             }
         }
     }
     public class test{
         public static void main(String[] args) throws Exception
         {
            Demo d= new Demo();
            Thread t1= new Thread(d);
            Thread t2= new Thread(d);        
            t1.start();
            t1.join();
            t2.start();
            for(int i=0;i<600;i++)
             {
                 System.out.println("main ********** ***"+i);
             }
        }    
     }
  • 相关阅读:
    springboot之session、cookie
    Springboot的异步线程池
    spring自带的定时任务功能@EnableScheduling
    SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递
    sss
    sss
    sss
    sss
    sss
    sss
  • 原文地址:https://www.cnblogs.com/exexex/p/8435244.html
Copyright © 2020-2023  润新知