java多线程读写分离

package com.jurassic.cloud.extract.api.out;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;

/**

  • @Author xiongm

  • @Date 2023/2/23 14:08

  • @Version 1.0
    */
    @RestController
    @Slf4j
    public class TestReadWirteApi {

    @GetMapping(value = “/test/t1”)
    public void test() {
    for (int i = 0; i < 10; i++) {
    new Thread(new Runnable() {
    @Override
    public void run() {
    CountDownLatch countDownLatch = new CountDownLatch(2);
    BlockingQueue<Map<String, Object>> blockingQueue = new ArrayBlockingQueue<>(100);
    long start = System.currentTimeMillis();
    TestReadWirteApi.log.info(Thread.currentThread().getName() + “start time :{}”, start);
    Map<Thread, Thread> threadThreadMap = new HashMap<>();
    Thread thread = new Thread(new ReaderRunable(Thread.currentThread().getName(), countDownLatch, blockingQueue, threadThreadMap));
    Thread thread2 = new Thread(new WriteRunable(Thread.currentThread().getName(), countDownLatch, blockingQueue, threadThreadMap));
    threadThreadMap.put(thread, thread2);
    thread.start();
    thread2.start();

                 try {
                     countDownLatch.await();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 TestReadWirteApi.log.info(Thread.currentThread().getName() + "end time :{}", System.currentTimeMillis());
                 TestReadWirteApi.log.info(Thread.currentThread().getName() + "huafei time :{}", System.currentTimeMillis() - start);
                 System.out.println(Thread.currentThread().getName() + "执行完成");
             }
         }, "task" + i).start();
     }
     System.out.println("主线程:" + Thread.currentThread().getName() + "执行完成");
    

    }

}

@Slf4j
class WriteRunable implements Runnable {

private Map<Thread, Thread> threadThreadMap;
private CountDownLatch countDownLatch;

private BlockingQueue<Map<String, Object>> blockingQueue;

private String taskThreadName;

WriteRunable(String taskThreadName, CountDownLatch countDownLatch, BlockingQueue<Map<String, Object>> blockingQueue, Map<Thread, Thread> threadThreadMap) {
    this.countDownLatch = countDownLatch;
    this.blockingQueue = blockingQueue;
    this.threadThreadMap = threadThreadMap;
    this.taskThreadName = taskThreadName;
}


@SneakyThrows
@Override
public void run() {
    Thread readThread = null;
    for (Thread thread : threadThreadMap.keySet()) {
        readThread = thread;
    }
    while (true) {
        Map<String, Object> take = blockingQueue.take();
        WriteRunable.log.info(taskThreadName + " :get:" + Thread.currentThread().getName() + take.toString());
        if (CollectionUtils.isEmpty(blockingQueue) && countDownLatch.getCount() == 1) {
            break;
        }
        if (readThread != null && readThread.isInterrupted() && CollectionUtils.isEmpty(blockingQueue)) {
            if (countDownLatch.getCount() == 2) {
                countDownLatch.countDown();
            }
            break;
        }
    }
    countDownLatch.countDown();
    System.out.println("WriteRunable线程:" + Thread.currentThread().getName() + "执行完成");


}

}

@Slf4j
class ReaderRunable implements Runnable {

private CountDownLatch countDownLatch;

private BlockingQueue<Map<String, Object>> blockingQueue;

private Map<Thread, Thread> threadThreadMap;

private String taskThreadName;

ReaderRunable(String taskThreadName, CountDownLatch countDownLatch, BlockingQueue<Map<String, Object>> blockingQueue, Map<Thread, Thread> threadThreadMap) {
    this.countDownLatch = countDownLatch;
    this.blockingQueue = blockingQueue;
    this.threadThreadMap = threadThreadMap;
    this.taskThreadName = taskThreadName;
}


@SneakyThrows
@Override
public void run() {

    Thread writeThread = null;
    for (Thread thread : threadThreadMap.values()) {
        writeThread = thread;
    }
    for (int i = 0; i < 1000; i++) {
        if (i / 100 == 0) {
            Thread.sleep(10);
        }
        //监听到写线程意外中断,读线程也终止
        if (writeThread != null && writeThread.isInterrupted()) {
            //让主线程countDownLatch释放1次
            if (countDownLatch.getCount() == 2) {
                countDownLatch.countDown();
            }
            break;
        }
        Map<String, Object> take = new HashMap<>();
        take.put(taskThreadName + ":put:" + Thread.currentThread().getName() + "key" + i, "value" + i);
        ReaderRunable.log.info(taskThreadName + ":put:" + Thread.currentThread().getName() + take.toString());
        blockingQueue.put(take);
    }
    System.out.println("ReaderRunable线程:" + Thread.currentThread().getName() + "执行完成");
    countDownLatch.countDown();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值