验证中...
Latest update 2019-06-12 23:37
design-single_src_com_eg_Main.java
Raw Copy
package com.eg;
/**
* @email 99723251@qq.com
* @author: zhangyangtao
* @date: 2019/6/12
* @time: 10:00 PM
*/
public class Main {
public static void main(String[] args) {
Lazy.getLazy();
}
}
design-single_design-single.iml
Raw Copy
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
design-single_.idea_modules.xml
Raw Copy
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/design-single.iml" filepath="$PROJECT_DIR$/design-single.iml" />
</modules>
</component>
</project>
design-single_src_com_eg_StaticInner.java
Raw Copy
package com.eg;
/**
* @email 99723251@qq.com
* @author: zhangyangtao
* @date: 2019/6/12
* @time: 11:01 PM
*/
public class StaticInner {
/**
* 静态内部类
*/
public static StaticInner getInstance() {
return StaticInnerV1.staticInner;
}
//继承懒汉式的优点,不调用不会加载
private static class StaticInnerV1 {
//static 只加载一次,线程安全
private static final StaticInner staticInner = new StaticInner();
}
public static void main(String[] args) {
System.out.println("程序启动");
StaticInner instance = StaticInner.getInstance();
StaticInner instance1 = StaticInner.getInstance();
System.out.println(instance==instance1);
}
}
design-single_src_com_eg_Lazy.java
Raw Copy
package com.eg;
import java.util.concurrent.TimeUnit;
/**
* @email 99723251@qq.com
* @author: zhangyangtao
* @date: 2019/6/12
* @time: 10:07 PM
*/
public class Lazy {
/**
* 单例--懒汉式
*/
private static Lazy lazy;
private Lazy() {
}
/**
* 在多线程的情况下会出现线程安全问题
*
* @return
*/
// public synchronized static Lazy getLazy() {
public static Lazy getLazy() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
if (lazy == null) {
lazy = new Lazy();
}
return lazy;
}
public static void main(String[] args) {
/**
* 测试在多线程情况下出现,出现线程安全问题
*/
for (int i = 0; i < 100; i++) {
new Thread(()->{
Lazy lazy = Lazy.getLazy();
Lazy lazy1 = Lazy.getLazy();
System.out.println(lazy==lazy1);
},String.valueOf(i)).start();
}
}
}
design-single_src_com_eg_DoubleLazy.java
Raw Copy
package com.eg;
import java.util.concurrent.TimeUnit;
/**
* @email 99723251@qq.com
* @author: zhangyangtao
* @date: 2019/6/12
* @time: 10:32 PM
*/
public class DoubleLazy {
/**
* 双重检验
*/
private volatile static DoubleLazy lazy;
private DoubleLazy() {
}
public static DoubleLazy getLazy() {
if (lazy == null) {
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (DoubleLazy.class){
if (lazy==null){
lazy = new DoubleLazy();
}
}
}
return lazy;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
DoubleLazy lazy = DoubleLazy.getLazy();
DoubleLazy lazy1 = DoubleLazy.getLazy();
System.out.println(lazy==lazy1);
},String.valueOf(i)).start();
}
}
}
design-single_.idea_misc.xml
Raw Copy
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
design-single_src_com_eg_Hungry.java
Raw Copy
package com.eg;
/**
* @email 99723251@qq.com
* @author: zhangyangtao
* @date: 2019/6/12
* @time: 9:50 PM
*/
public class Hungry {
/**
* 单例--饿汉式
*/
/**
* 饿汉式优点:先天性线程安全,当类初始化的时候就被创建对象
* 饿汉式缺点:项目在启动的时候会变得非常慢,占用方法区大量内存,如果用户不使用该对象的时候,提前创建好,也是非常浪费资源的。
* ps:方法区:静态变量、常量、已被虚拟机加载的类信息、及时编译器编译后的代码
*/
private static Hungry hungry = new Hungry();
private Hungry() {
}
public static Hungry getLazy() {
return hungry;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_you_jiang_zheng_wen Zheng_wen_close