参考资料:
高并发Java(7):并发设计模式 http://www.importnew.com/21312.html
1.单实例模式的线程安全的两种上佳实践:
public class SQLFactory
{
private Logger logger = LoggerFactory.getLogger(getClass());//Log.getLog(getClass());
/*//old method not thread-safe
private static SQLFactory instance = null;
public static SQLFactory getInstance() {
if (instance == null) {
instance = new SQLFactory();
}
return instance;
}
*/
//thread-safe best practice
public static SQLFactory getInstance() {
return SQLHolder.instance;
}
private static class SQLHolder {
private static SQLFactory instance = new SQLFactory();
}
/*//also good thread-safe
volatile private static SQLFactory instance = null;
public static SQLFactory getInstance() {
synchronized (SQLFactory.class) {
if (instance == null) {
instance = new SQLFactory();
}
}
return instance;
}*/
}
2.测试方法:
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.highgo.admin.migrator.controller.SQLFactory;
public class TestThreadSafe {
public void doing() {
System.out.println(SQLFactory.getInstance().hashCode());
}
public static void main(String[] args) {
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(4, 8, 101, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(2000));
final TestThreadSafe tf = new TestThreadSafe();
for (int i = 0; i < 10; i++) {
threadPool.submit(() -> {
tf.doing();
});
}
}
}
3.测试结果:相同的实例哈希值表示始终都是同一个实例。
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302