[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/cuteElf/JavaGuide/master/docs/java/Multithread/synchronized.md [Back]  [Original]


![Synchronized JDK1.6  ReenTrantLock ](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/Java%20%E7%A8%8B%E5%BA%8F%E5%91%98%E5%BF%85%E5%A4%87%EF%BC%9A%E5%B9%B6%E5%8F%91%E7%9F%A5%E8%AF%86%E7%B3%BB%E7%BB%9F%E6%80%BB%E7%BB%93/%E4%BA%8C%20%20Synchronized%20%E5%85%B3%E9%94%AE%E5%AD%97%E4%BD%BF%E7%94%A8%E3%80%81%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86%E3%80%81JDK1.6%20%E4%B9%8B%E5%90%8E%E7%9A%84%E5%BA%95%E5%B1%82%E4%BC%98%E5%8C%96%E4%BB%A5%E5%8F%8A%20%E5%92%8CReenTrantLock%20%E7%9A%84%E5%AF%B9%E6%AF%94.png)

###  synchronized

- ****
- ****  static newA synchronized B synchronized ** synchronized  synchronized **
- ****  synchronized synchronized(this)synchronized  static  synchronized(class) Class synchronized static  synchronized(String a) JVM

 synchronized 




****

```java
public class Singleton {

    private volatile static Singleton uniqueInstance;

    private Singleton() {
    }

    public static Singleton getUniqueInstance() {
       //
        if (uniqueInstance == null) {
            //
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
}
```
 uniqueInstance  volatile 

uniqueInstance  volatile  uniqueInstance = new Singleton(); 

1.  uniqueInstance 
2.  uniqueInstance
3.  uniqueInstance 

 JVM  1->3->2 T1  1  3 T2  getUniqueInstance()  uniqueInstance  uniqueInstance uniqueInstance 

 volatile  JVM 


###synchronized 


**synchronized  JVM **

** synchronized **

```java
public class SynchronizedDemo {
	public void method() {
		synchronized (this) {
			System.out.println("synchronized ");
		}
	}
}

```

 JDK  javap  SynchronizedDemo  `javac SynchronizedDemo.java`  .class `javap -c -s -v -l SynchronizedDemo.class`

![synchronized ](https://images.gitbook.cn/abc37c80-d21d-11e8-aab3-09d30029e0d5)



**synchronized  monitorenter  monitorexit  monitorenter monitorexit **  monitorenter  monitor(monitorJavasynchronized Java) .011 monitorexit 0

** synchronized **

```java
public class SynchronizedDemo2 {
	public synchronized void method() {
		System.out.println("synchronized ");
	}
}

```

![synchronized ](https://images.gitbook.cn/7d407bf0-d21e-11e8-b2d6-1188c7e0dd7e)

synchronized  monitorenter  monitorexit  ACC_SYNCHRONIZED JVM  ACC_SYNCHRONIZED 


 Java synchronized monitor Mutex Lock Java  synchronized  Java 6  Java  JVM synchronized  synchronized JDK1.6


###  JDK1.6 

JDK1.6 



****

** CAS **

JavaJVM13



** **

(1.6)**CAS** JavaJVM13

** CAS CAS**

**  **



/

**/** ****



> ""

 JDK1.6 `--XX:+UseSpinning`JDK1.61.6**10`--XX:PreBlockSpin`**

,** JDK1.6 **

** **



** **





###  Synchronized  ReenTrantLock 


** **

10

** synchronized  JVM  ReenTrantLock  API**

synchronized  JVM   JDK1.6  synchronized ReenTrantLock  JDK  API  lock()  unlock  try/finally 

** ReenTrantLock  synchronized **

synchronizedReenTrantLock****

- **ReenTrantLock**lock.lockInterruptibly()
- **ReenTrantLocksynchronized** ReenTrantLock ReenTrantLock`ReentrantLock(boolean fair)`
- synchronizedwait()notify/notifyAll()/ReentrantLockConditionnewCondition() ConditionJDK1.5LockCondition**Condition notify/notifyAll() JVM ReentrantLockCondition** ConditionsynchronizedLockConditionnotifyAll()ConditionsignalAll() Condition

ReenTrantLock

**synchronized  ReenTrantLock  finally  unlock**

** **

JDK1.6synchronized  ReenTrantLock synchronized ReenTrantLock  synchronized  JDK1.6  JVM  synchronized **JDK1.6 synchronized  ReenTrantLock  ReenTrantLock JDK1.6synchronizedReenTrantLocksynchronizedsynchronizedsynchronizedsynchronizedReenTrantLockCAS**

Web Proxy Viewer  |  New URL  |  Original Page