作家
登录

Java多线程之synchronized关键字详解

作者: 来源: 2017-05-29 14:39:09 阅读 我要评论

  •         example = new Example(); 
  •  
  •         Thread t2 = new Thread2(example); 
  •  
  •         t1.start(); 
  •         t2.start(); 
  •     } 
  •  
  •  
  • class Example 
  •     public synchronized static void execute() 
  •     { 
  •         for (int i = 0; i < 20; ++i) 
  •         { 
  •             try 
  •             { 
  •                 Thread.sleep((long) Math.random() * 1000); 
  •             } 
  •             catch (InterruptedException e) 
  •             { 
  •                 e.printStackTrace(); 
  •             } 
  •             System.out.println("Hello: " + i); 
  •         } 
  •     } 
  •  
  •     public synchronized static void execute2() 
  •     { 
  •         for (int i = 0; i < 20; ++i) 
  •         { 
  •             try 
  •             { 
  •                 Thread.sleep((long) Math.random() * 1000); 
  •             } 
  •             catch (InterruptedException e) 
  •             { 
  •                 e.printStackTrace(); 
  •             } 
  •             System.out.println("World: " + i); 
  •         } 
  •     } 
  •  
  •  

      推荐阅读

      PHP与Golang如何通信?

    惯例的筹划: 用Golang写一个http/TCP办事,php经由过程http/TCP与Golang通信 将Golang经由较多封装,做为php扩大。 PHP经由过程体系敕令,调取Golang的可履行文件存在的问题: >>>详细阅读


    本文标题:Java多线程之synchronized关键字详解

    地址:http://www.17bianji.com/lsqh/35484.html

  • 关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)