References:
Thread-safe singleton mode under high concurrency (the most complete and classic)
High Concurrency Java (7): Concurrency Design Patterns http://www.importnew.com/21312.html
1. Two best practices for thread safety in single instance mode:
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. Test method:
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. Test results: The same instance hash value means that it is always the same instance.
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302
1090303302