• 技术文章 >java >java基础

    java中linkedblockingqueue的增加方法

    小妮浅浅小妮浅浅2021-01-11 15:14:41原创2219

    在linkedblockingqueue的增加元素上,也我们之前所学的ArrayBlockingQueue有相通的地方。这里我们以其中的put方法进行举例,需要加入一些锁的使用对队列进行约束。下面就linkedblockingqueue增加元素的方法进行put的使用事项讲解,同时展示出对应的代码实例。

    1.put方法使用事项

    1)使用putLock加锁;

    2)如果队列满了就阻塞在notFull条件上;

    3)否则就入队;

    4)如果入队后元素数量小于容量,唤醒其它阻塞在notFull条件上的线程;

    5)释放锁;

    6)如果放元素之前队列长度为0,就唤醒notEmpty条件;

    2.put增加元素实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    public void put(E e) throws InterruptedException {

            if (e == null) throw new NullPointerException();   //e不能为null

            int c = -1;

            Node<E> node = new Node<E>(e);

            final ReentrantLock putLock = this.putLock;     //获取put锁

            final AtomicInteger count = this.count;          //获取count

            putLock.lockInterruptibly();

            try {

                while (count.get() == capacity) {        //如果满了,那么就需要使用notFull阻塞

                    notFull.await();

                }

                enqueue(node);

                c = count.getAndIncrement();

                if (c + 1 < capacity)                    //如果此时又有空间了,那么notFull唤醒

                    notFull.signal();

            } finally {

                putLock.unlock();             //释放锁

            }

            if (c == 0)            //当c为0时候,也要根take锁说一下,并发下

                signalNotEmpty();        //调用notEmpty       

        }

    public E take() throws InterruptedException {

        E x;

        int c = -1;

        final AtomicInteger count = this.count;

        final ReentrantLock takeLock = this.takeLock;

        // 使用takeLock加锁

        takeLock.lockInterruptibly();

        try {

            // 如果队列无元素,则阻塞在notEmpty条件上

            while (count.get() == 0) {

                notEmpty.await();

            }

            // 否则,出队

            x = dequeue();

            // 获取出队前队列的长度

            c = count.getAndDecrement();

            // 如果取之前队列长度大于1,则唤醒notEmpty

            if (c > 1)

                notEmpty.signal();

        } finally {

            // 释放锁

            takeLock.unlock();

        }

        // 如果取之前队列长度等于容量

        // 则唤醒notFull

        if (c == capacity)

            signalNotFull();

        return x;

    }

      

    private E dequeue() {

        // head节点本身是不存储任何元素的

        // 这里把head删除,并把head下一个节点作为新的值

        // 并把其值置空,返回原来的值

        Node<E> h = head;

        Node<E> first = h.next;

        h.next = h; // help GC

        head = first;

        E x = first.item;

        first.item = null;

        return x;

    }

      

    private void signalNotFull() {

        final ReentrantLock putLock = this.putLock;

        putLock.lock();

        try {

            // 唤醒notFull

            notFull.signal();

        } finally {

            putLock.unlock();

        }

    }

    以上就是java中linkedblockingqueue增加元素的方法,相信经过之前ArrayBlockingQueue的学习,以及结合本篇put使用事项的了解,大家已经能够学会对于其入队的相关操作了。

    专题推荐:java linkedblockingqueue增加
    上一篇:linkedblockingqueue在java中的原理 下一篇:linkedblockingqueue在java中出队

    相关文章推荐

    • ArrayBlockingQueue在java的入队• java中ArrayBlockingQueue的出队• java ArrayBlockingQueue的方法及不足点• java中linkedblockingqueue用法• linkedblockingqueue在java中的原理

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网