/** * * @author devin * @date 2021-01-08 11:12 * @version 1.0.2 */ public class SingleTest { private static volatile SingleTest instance ; private SingleTest (){ System . out . println ( "create method" ); } //在这里加同步太重 publi
*
* @author devin
* @date 2021-01-08 11:12
* @version 1.0.2
*/
public class SingleTest {
private static volatile SingleTest instance;
private SingleTest(){
System.out.println("create method");
}
//在这里加同步太重
public static SingleTest getInstance(){
if(instance == null){
synchronized (SingleTest.class){
if(instance == null){
return instance = new SingleTest();
}
}
}
return instance;
}
public static void main(String[] args) {
for(int i = 0;i < 1000;i++){
new Thread(){
public void run() {
SingleTest.getInstance();
}
}.start();
}
}
}