单例模式场景模拟和问题解决

饿汉式单例

private static Student student = new Student();

不存在线程安全问题

懒汉式单例

线程安全问题

package org.example.Singleton;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class SingletonTest {
    private static Student student = new Student();

    private static Car car = null;

    private static AtomicInteger count = new AtomicInteger(0);

    private static ExecutorService threadPoolExecutor = Executors.newCachedThreadPool();

    private static final CountDownLatch latch = new CountDownLatch(15);

    SingletonTest() throws InterruptedException {
        threadNonSafeLoad();
    }

    public static void main(String[] args) throws InterruptedException {
        loadInstance();
        latch.await();
        System.out.println(count.get());
    }

    private static void threadSafeLoad() {

    }

    private void threadNonSafeLoad() {
        //        System.out.println(this.car);
        if (this.car == null) {
            count.addAndGet(1);
            this.car = new Car();
        }
        latch.countDown();
    }

    private static void loadInstance() {
        for (int i = 0; i < 15; i++) {
//            Thread.sleep(50);
            Thread thread = new Thread(() -> {
                try {
                    new SingletonTest();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            threadPoolExecutor.execute(thread);
        }
    }
}

class Student {
    private String name;
}

class Car {
    private String name;
}

运行结果:

会多次创建`Car`对象
1~15

解决方法-双重判断

    private void threadSafeLoad() {
        if (this.car == null) {
            // 避免每次都加锁进行判断
            synchronized (SingletonTest.class) {
                if (this.car == null) {
                    count.addAndGet(1);
                    this.car = new Car();
                }
            }
        }
        latch.countDown();
    }
    SingletonTest() throws InterruptedException {
//        threadNonSafeLoad();
        threadSafeLoad();
    }

运行结果:

1

相关推荐

  1. 模式场景模拟问题解决

    2024-07-14 02:38:02       21 阅读
  2. 深度解析模式

    2024-07-14 02:38:02       36 阅读
  3. 模式模板

    2024-07-14 02:38:02       39 阅读
  4. 【Python】模式工厂模式

    2024-07-14 02:38:02       30 阅读
  5. 模式模板

    2024-07-14 02:38:02       62 阅读
  6. 模式---饿汉模式懒汉模式

    2024-07-14 02:38:02       37 阅读
  7. 常用的设计模式模式工厂模式

    2024-07-14 02:38:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 02:38:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 02:38:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 02:38:02       57 阅读
  4. Python语言-面向对象

    2024-07-14 02:38:02       68 阅读

热门阅读

  1. IOS-Share Extension

    2024-07-14 02:38:02       15 阅读
  2. Android系统实现多网共存且能独立上外网

    2024-07-14 02:38:02       20 阅读
  3. 文件上传到阿里云

    2024-07-14 02:38:02       18 阅读
  4. 系统服务综合案例

    2024-07-14 02:38:02       18 阅读
  5. 面试迟到了怎么办

    2024-07-14 02:38:02       17 阅读
  6. python +=d的底层实现

    2024-07-14 02:38:02       15 阅读
  7. Vue3 watch与watchEffect的区别

    2024-07-14 02:38:02       18 阅读
  8. Shell学习day1

    2024-07-14 02:38:02       18 阅读
  9. QML教程-10分钟一口气快速掌握QML基础元素

    2024-07-14 02:38:02       17 阅读