读写锁、队列

ReadWriteLock

image-20230307151624122

package com.cz;

import java.util.HashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author 卓亦苇
 * @version 1.0
 * 2023/3/6 15:57
 */
public class ReadWriteLockTest {
    public static void main(String[] args) {
        Cache cache = new Cache();
        for (int i = 1; i < 10; i++) {
            int finalI = i;
            new Thread(()->{

                cache.write(finalI+"",finalI);
                cache.read(Thread.currentThread().getName());

        },String.valueOf(i)).start();
        }
        for (int i = 1; i < 10; i++) {
            int finalI = i;
            new Thread(()->{
                cache.read(finalI+"");

            },String.valueOf(i)).start();
        }
    }
}
class Cache{
    HashMap<String,Object> hashmap = new HashMap<>();
    ReadWriteLock readWriteLock=new ReentrantReadWriteLock();


    public void write(String key,Object value){
        readWriteLock.writeLock().lock();
       try {
           System.out.println(Thread.currentThread().getName()+"写入"+value);
           hashmap.put(key,value);
           System.out.println(Thread.currentThread().getName()+"写入完成");
       }finally {
           readWriteLock.writeLock().unlock();
       }
    }
    public void read(String key){
        readWriteLock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"读出");
            hashmap.get(key);
            System.out.println(Thread.currentThread().getName()+"读出完成");
        }finally {
            readWriteLock.readLock().unlock();
        }

    }
}

独占锁(写锁)一次只能被一个线程占有

共享锁(读锁)多个线程可以同时占有ReadwriteLock

读-读可以共存!
读-写不能共存!
写-写不能共存!

BlockingQueue

image-20230307155912442

image-20230307161212481

image-20230307172221977

四组API

方式 抛出异常 有返回值,不抛出异常 阻塞等待 超时等待
添加 add offer put offer
移除 remove poll take poll
判断队列首 element peek
package com.cz.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author 卓亦苇
 * @version 1.0
 * 2023/3/7 16:00
 */
public class Test {
    public static void main(String[] args) throws InterruptedException {
        //BlockingQueue阻塞队列
        test4();

    }
    public static void test1(){
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);//容量参数
        //有返回值类型的阻塞队列
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println(blockingQueue.add("c"));

        System.out.println("===========");
        //查看队列首部元素
        System.out.println(blockingQueue.element());
        System.out.println("===========");

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
    }

    public static void test2(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        //有返回值的阻塞队列
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));

        System.out.println("==========");
        System.out.println(blockingQueue.peek());
        System.out.println("==========");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());

    }

    public static void test3() throws InterruptedException {
        //阻塞等待
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        blockingQueue.put("a");
        blockingQueue.put("a");
        blockingQueue.put("a");
        blockingQueue.put("a");

        System.out.println("==========");

        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.peek());


    }

    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        //超时等待的阻塞队列
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d",4,TimeUnit.SECONDS));

        System.out.println("==========");
        System.out.println(blockingQueue.peek());
        System.out.println("==========");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(4,TimeUnit.SECONDS));

    }
}

SynchronousQueue同步队列

没有容量,
进去一个元素,必须等待取出来之后,才能再往里面放一个元素!

package com.cz.bq;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;


/**
 * @author 卓亦苇
 * @version 1.0
 * 2023/3/7 18:24
 */

/**
 * 同步队列
 * 和其他的BLockingQueue不一样,SynchronousQueue不存储元素
 * put了一个元素,必须从里面先take取出来,否则不能在put进去值!
 */
public class synchronousQueue {
    public static void main(String[] args) {
        BlockingQueue<String> synchronousQueue =new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"写入了a");
                synchronousQueue.put("a");
                System.out.println(Thread.currentThread().getName()+"写入了b");
                synchronousQueue.put("b");
                System.out.println(Thread.currentThread().getName()+"写入了c");
                synchronousQueue.put("c");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        new Thread(()->{

            try {
                System.out.println(Thread.currentThread().getName()+"输出"+synchronousQueue.take());
                System.out.println(Thread.currentThread().getName()+"输出"+synchronousQueue.take());
                System.out.println(Thread.currentThread().getName()+"输出"+synchronousQueue.take());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }).start();
    }
}
posted @   卓亦苇  阅读(15)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示