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();
}
}