Volatile和Synchronized双重校验锁实现线程安全的单例模式
package com.kk.first.thread;
public class TestSingle {
private static volatile TestSingle testSingle;
private TestSingle() {}
public static TestSingle getInstance() {
if (testSingle == null) {
synchronized (TestSingle.class) {
if (testSingle == null) {
testSingle = new TestSingle();
}
}
}
return testSingle;
}
}
synchronized相关详解Java高并发之synchronized关键字
volatile相关详解Java高并发之volatile 关键字