作家
登录

Java多线程之synchronized关键字详解

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

  •                 Thread.sleep((long) Math.random() * 1000); 
  •             } 
  •             catch (InterruptedException e) 
  •             { 
  •                 e.printStackTrace(); 
  •             } 
  •             System.out.println("World: " + i); 
  •         } 
  •     } 
  •  
  •  
  • class Thread1 extends Thread 
  •     private Example example; 
  •  
  •     public Thread1(Example example) 
  •     { 
  •         this.example = example; 
  •     } 
  •  
  •     @Override 
  •     public void run() 
  •     { 
  •         example.execute(); 
  •     } 
  •  
  •  
  • class Thread2 extends Thread 
  •     private Example example; 
  •  
  •     public Thread2(Example example) 
  •     { 
  •         this.example = example; 
  •     } 
  •  
  •     @Override 
  •     public void run() 
  •     { 
  •         example.execute2(); 
  •     } 
  •  
  • 如不雅去掉落synchronized关键字,则两个办法并发履行,并没有互相竽暌拱响。

    然则如例子法度榜样中所写,即就是两个办法:

    1. public class ThreadTest 
    2.     public static void main(String[] args) 
    3.     { 
    4.         Example example = new Example(); 
    5.  
    6.         Thread t1 = new Thread1(example); 
    7.         Thread t2 = new Thread2(example); 
    8.  
    9.         t1.start(); 
    10.         t2.start(); 
    11.     } 

    12.   推荐阅读

        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)