码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程synchronized关键字

时间:2017-05-18 20:26:54      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:print   div   代码   getname   zed   his   java   sum   关键字   

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关键字

标签:print   div   代码   getname   zed   his   java   sum   关键字   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!