当前位置 : 主页 > 编程语言 > java >

单例模式 Java版

来源:互联网 收集:自由互联 发布时间:2022-07-07
/** * * @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(){
@Override
public void run() {
SingleTest.getInstance();
}
}.start();
}

}
}


上一篇:Java Stream流
下一篇:没有了
网友评论