标签: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--); } } }
标签:print div 代码 getname zed his java sum 关键字
原文地址:http://www.cnblogs.com/qsdy/p/6874916.html