LinkBlockingQueue用法记录

本文记录了在多线程环境下使用LinkBlockingQueue时,如何通过添加读写锁来确保线程安全。示例展示了未加锁与加锁后的不同输出结果,加锁后能避免数据不一致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多线程 使用LinkBlockingQueue最好添加读写锁

以下示例,不加读写锁有可能线程C会出现输出为false的情况

添加以后则不会出现此种状况

package com.cy.BlockQueueDemo;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LinkBlockingQueueDemo {


    private static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue();

    private static ReentrantReadWriteLock.WriteLock writeLock = new ReentrantReadWriteLock().writeLock();

    public static void main(String[] args) {

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+":"+queue.toString());

            writeLock.lock();
            for (int i = 1; i <= 10; i++) {
                try {
                    Thread.sleep(1000);
                    queue.add(i);
                    System.out.println(Thread.currentThread().getName()+":"+queue.toString());

                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    ;
                }
            }
            writeLock.unlock();
            System.out.println(Thread.currentThread().getName()+":"+queue.toString());

        }, "Thread-A").start();

        new Thread(() -> {

            while(true){
                try {
                    writeLock.lock();
                    if(queue.contains(5)){
                        System.out.println("come into "+Thread.currentThread().getName());
                        Thread.sleep(500);
                        queue.take();
                        System.out.println(Thread.currentThread().getName()+":"+queue.toString());
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    ;
                }finally {
                    writeLock.unlock();
                }
            }


        }, "Thread-B").start();

        new Thread(() -> {
            while (true) {
                try {
                    writeLock.lock();
                    if(queue.contains(5)){
                        System.out.println("come into "+Thread.currentThread().getName());
                        Thread.sleep(5000);
                        boolean remove = queue.remove(5);
                        System.out.println(Thread.currentThread().getName()+":"+queue.toString());
                        System.out.println(remove);
                    }
                    writeLock.unlock();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    ;
                }
            }


        }, "Thread-C").start();
    }
}

随机输出结果1:
Thread-A:[]
Thread-A:[1]
Thread-A:[1, 2]
Thread-A:[1, 2, 3]
Thread-A:[1, 2, 3, 4]
Thread-A:[1, 2, 3, 4, 5]
Thread-A:[1, 2, 3, 4, 5, 6]
Thread-A:[1, 2, 3, 4, 5, 6, 7]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
come into Thread-B
Thread-B:[2, 3, 4, 5, 6, 7, 8, 9, 10]
come into Thread-B
Thread-B:[3, 4, 5, 6, 7, 8, 9, 10]
come into Thread-B
Thread-B:[4, 5, 6, 7, 8, 9, 10]
come into Thread-B
Thread-B:[5, 6, 7, 8, 9, 10]
come into Thread-B
Thread-B:[6, 7, 8, 9, 10]

 

随机输出结果2:
Thread-A:[]
Thread-A:[1]
Thread-A:[1, 2]
Thread-A:[1, 2, 3]
Thread-A:[1, 2, 3, 4]
Thread-A:[1, 2, 3, 4, 5]
Thread-A:[1, 2, 3, 4, 5, 6]
Thread-A:[1, 2, 3, 4, 5, 6, 7]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Thread-A:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
come into Thread-C

Thread-C:[1, 2, 3, 4, 6, 7, 8, 9, 10]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值