JavaEE——No.1 多线程案例

JavaEE传送门

JavaEE

JavaEE——No.1 线程安全问题

JavaEE——No.2 线程安全问题



多线程案例

1. 单例模式

单例模式是一种常见的设计模式.

设计模式: 软件开发时, 会遇到一些常见的 “问题场景”, 针对这些问题, 就有大佬们整理出来了一些相对应的解决办法, 供其他人来研究学习.

单例

指单个实例, 某个类, 有且只有一个实例. 有些场景, 要求实例不能有多个, 通过单例模式, 相当于对 “单个实例” 做了一个更加严格的约束.

比如JDBC中的 DataSource 实例就只需要一个.

单例模式具体的实现方式, 又分为 “饿汉模式” 和 “懒汉模式” 两种.

static (Java 中天然的单个实例)

static 修饰的成员/属性, 变成了类成员/类属性. 当属性变成类属性的时候, 此时就是一个 “单个实例”

class Singleton {
    private static Singleton instance = new Singleton();
}

上述代码中的 instance 就是 Signleton 这个类的唯一实例.


饿汉模式

类加载的同时, 创建实例.

class Singleton {
    private static Singleton instance = new Singleton();
    
    public Singleton getInstance() {
        return instance;
    }
    
    //把构造方法设为私有的, 此时在类的外面, 就无法 new 实例了
    private Singleton() {
		//强制禁止其他的类中, new Singleton 实例
        //唯一的入口就是通过 getInstance 方法
    }
}

懒汉模式

类加载的时候不创建实例. 第一次使用的时候才创建实例

(创建实例的实际更迟, 效率更高)

#单线程版

class SingletonLazy {
    private static SingletonLazy instance = null;

    public static SingletonLazy getInstance() {
        if (instance == null) {
            //首次调用instance的时候才会触发
            instance = new SingletonLazy();
        }
        return instance;
    }

    private SingletonLazy() {
        
    }
}

public class Test {
    public static void main(String[] args) {
        SingletonLazy instance = SingletonLazy.getInstance();
        //如果在整个代码中后续没人调用 getInstance, 这样就节省了构造实例的过程
    }
}

# 注意 #

在上述代码中, getInstance 方法中, 及涉及到了读, 也涉及到了写, 是不安全的.

如下图 t2 线程进行 LOAD 的时候, t1 还没有进行修改完, t2 读到的就还是之前的值, 就会导致同时创建两个实例.

#多线程版

我们这时使用 synchronized 来改善这里的线程安全问题

class SingletonLazy {
    private static SingletonLazy instance = null;

    public static SingletonLazy getInstance() {   
        synchronized (SingletonLazy.class) { 
            if (instance == null) {
                instance = new SingletonLazy();
            }
        }

        return instance;
    }

    private SingletonLazy() {}
}

但这时, 我们只要调用 getInstance 方法, 就会进行加锁, 加锁绝对不是无脑加的. 加锁的开销还是比较大的.

实例创建之前, 是线程不安全的, 需要加锁. 实例创建之后, 是线程安全的, 就不需要加锁了.

#多线程改进版

因此, 我们可以在加锁的外层, 再加上一层判定.

class SingletonLazy {
    private static SingletonLazy instance = null;

    public static SingletonLazy getInstance() {
        // 判定是否要加锁
        if (instance == null) {
            synchronized (SingletonLazy.class) {
                // 判定是否要创建实例
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy() {}
}

但这时可能还会出现指令重排序的问题, 导致线程不安全

new 操作的本质分为三个步骤

  1. 申请内存, 得到内存首地址
  2. 调用构造方法来初始化实例
  3. 把内存的首地址赋值给 instance 引用

比如我们在进行

instance = new SingletonLazy();

操作时, 在单线程的角度下, 2 和 3 是可以调换顺序的. (在单线程的情况下, 此时 2 和 3 先执行谁, 后执行谁效果是一样的)

假设此处触发了指令重排序, 有可能 t1 执行了 1 和 3 之后 (得到了不完全的对象, 只是有内存, 内存上的数据无效) , 执行 2 之前

这时 t2 线程调用了这个 getlnstance ,这个 getlnstance 就会认为这个 Instance 非空, 就直接返回了.

并且在后续可能会针对 Instance 进行解引用操作 (使用里面的属性/ 方法).

#多线程最终版

我们想要禁止指令重排序, 需要使用 volatile 关键字

class SingletonLazy {
    //禁止指令重排序
    private volatile static SingletonLazy instance = null;

    public static SingletonLazy getInstance() {
        if (instance == null) {
            synchronized (SingletonLazy.class) {

                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy() {}
}

2. 阻塞队列

  1. 阻塞队列是一个特殊的队列, 遵循先进先出的原则
  2. 是线程安全的
  3. 如果队列满, 继续入队列, 入队列操作就会阻塞. 直到队列不满, 入列才能完成
  4. 如果队列空, 继续出队列, 出队列操作就会阻塞, 直到队列不空, 出队列才能完成.

阻塞队列的应用场景: 生产者消费者模型

生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题。

生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取.

1) 阻塞队列使生产者和消费者之间解耦合, 生产者和消费者就不需要知道对方的存在.

2) 削峰填谷 (按照没有生产者消费者模型的写法, 外面流量过来的压力, 就会直接压到每个服务器上, 如果某个服务器抗压能力不行, 就容易挂)

此时的阻塞队列, 就像是三峡的水坝, 如果上游水大, 就关闸蓄水. 如果上游水少, 就开闸放水.


阻塞队列的使用

Java 标准库中内置了阻塞队列.

  • BlockingQueue 是一个接口

队列有三个基本操作: 入队列, 出队列, 取队首元素 (阻塞队列没有专门的带阻塞的取队首元素)

  • 入队列 .put( )

(阻塞队列也有入队列也可以使用 offer 方法, 但并不推荐使用, 因为其不带有阻塞特性)

// 带阻塞功能
blockingQueue.put(1);
blockingQueue.put(2);
blockingQueue.put(3);
  • 出队列 .take( )
Integer ret = blockingQueue.take();
System.out.println(ret);
ret = blockingQueue.take();
System.out.println(ret);
ret = blockingQueue.take();
System.out.println(ret);
  • 阻塞

当我们在上述操作中, 已经把1, 2, 3 全部取出的时候, 在进程 take( ) 操作就会进行阻塞.


阻塞队列的实现

  • 通过数组实现
  • 入队列和出队列方法中都涉及到修改, 防止出现多线程安全问题, 使用 synchronized 进行加锁
  • put 插入元素时, 如果队列满了, 进行 wait
  • take 取出元素时, 如果队列为空, 进行 wait
  • 一个队列, 不会出现又是空又是满的情况, 所以不需要两个锁对象
  • wait 外面需要加上 while 循环, 在多线程的情况下, 被唤醒时不一定队列就满或者不满
class MyBlockingQueue {
    private int[] items = new int[1000];
    private volatile int head = 0;
    private volatile int tail = 0;
    private volatile int size = 0;

    //入队列
    public void put(int elem) throws InterruptedException {
        synchronized (this) {
            //队列满了, 进行 wait
            while (size >= items.length) {
                this.wait();
            }

            items[tail] = elem;
            tail++;
            if(tail >= items.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }
    }

    // 出队列
    public Integer take() throws InterruptedException {
        synchronized (this) {
            //队列为空, 进行 wait
            while (size == 0) {
                this.wait();
            }

            int ret = items[head];
            head++;
            if(head >= items.length) {
                head = 0;
            }
            size--;
            this.notify();
            return ret;
        }
    }
}

🌷(( ◞•̀д•́)◞⚔◟(•̀д•́◟ ))🌷

以上就是今天要讲的内容了,希望对大家有所帮助,如果有问题欢迎评论指出,会积极改正!!
在这里插入图片描述
加粗样式

这里是Gujiu吖!!感谢你看到这里🌬
祝今天的你也
开心满怀,笑容常在。
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值