首页 > 编程语言 > 详细

Java多线程synchronized关键字

时间:2017-05-18 19:31:15      阅读:189      评论:0      收藏:0      [点我收藏+]

synchronized关键字代表着同步的意思,在Java中被synchronized修饰的有三种情况

1.同步代码块

//锁为obj
synchronized(obj){ while(true){ if(product > 0){ System.out.println(Thread.currentThread().getName()+"消费:"+product--); } } }

 

2.同步函数

//锁为this
public synchronized void consume() { while(true){ if(product > 0){ System.out.println(Thread.currentThread().getName()+"消费:"+product--); } } }

  

3.静态同步函数

//锁为this.getClass()
static public synchronized void consume() { while(true){ if(product > 0){ System.out.println(Thread.currentThread().getName()+"消费:"+product--); } } }

  

Java多线程synchronized关键字

原文:http://www.cnblogs.com/qsdy/p/6874916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!