From 0255d7420d013aa50dcbfa3bdb9b0e84a4f0ff2c Mon Sep 17 00:00:00 2001 From: hz21056617 <1IB76X4g7T> Date: Sat, 29 Jan 2022 18:07:54 +0800 Subject: [PATCH] 111 --- concurrent/pom.xml | 28 ++++++ .../queue/BlockingQueueTestWork.java | 52 +++++++++++ .../reentrantlock/ReentrantLockFairTest.java | 92 +++++++++++++++++++ .../ReentrantLockNonFairTest.java | 91 ++++++++++++++++++ .../test/java/concurrent/queue/QueueTest.java | 40 ++++++++ .../reentrantlock/ReentrantLockTest.java | 45 +++++++++ elasticsearch/pom.xml | 19 ++++ pom.xml | 10 +- 8 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 concurrent/pom.xml create mode 100644 concurrent/src/main/java/concurrent/queue/BlockingQueueTestWork.java create mode 100644 concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockFairTest.java create mode 100644 concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockNonFairTest.java create mode 100644 concurrent/src/test/java/concurrent/queue/QueueTest.java create mode 100644 concurrent/src/test/java/concurrent/reentrantlock/ReentrantLockTest.java create mode 100644 elasticsearch/pom.xml diff --git a/concurrent/pom.xml b/concurrent/pom.xml new file mode 100644 index 0000000..a829cdc --- /dev/null +++ b/concurrent/pom.xml @@ -0,0 +1,28 @@ + + + + JavaBasiceDemo + com.wyl.example + 1.0-SNAPSHOT + + 4.0.0 + + concurrent + + + 8 + 8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + + + + \ No newline at end of file diff --git a/concurrent/src/main/java/concurrent/queue/BlockingQueueTestWork.java b/concurrent/src/main/java/concurrent/queue/BlockingQueueTestWork.java new file mode 100644 index 0000000..e7f6e56 --- /dev/null +++ b/concurrent/src/main/java/concurrent/queue/BlockingQueueTestWork.java @@ -0,0 +1,52 @@ +package concurrent.queue; + +import lombok.AllArgsConstructor; +import lombok.SneakyThrows; + +import java.util.concurrent.ArrayBlockingQueue; + +@AllArgsConstructor +public class BlockingQueueTestWork extends Thread { + ArrayBlockingQueue arrayBlockingQueue; + Integer methodNumber; + + @SneakyThrows + @Override + public void run() { + switch (methodNumber) { + case 1: + offerTest(); + break; + case 2: + addTest(); + break; + case 3: + putTest(); + break; + } + } + + public void offerTest() { + boolean offer = arrayBlockingQueue.offer(1); + if (!offer) { + System.out.println("队列满了"); + return; + } + System.out.println("添加成功"); + } + + public void addTest() { + boolean offer = arrayBlockingQueue.add(1); + if (!offer) { + System.out.println("队列满了"); + return; + } + System.out.println("添加成功"); + } + + public void putTest() throws InterruptedException { + arrayBlockingQueue.put(1); + System.out.println("添加成功"); + } +} + diff --git a/concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockFairTest.java b/concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockFairTest.java new file mode 100644 index 0000000..fd7bce9 --- /dev/null +++ b/concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockFairTest.java @@ -0,0 +1,92 @@ +package concurrent.reentrantlock; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.SneakyThrows; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * ReentrantLock 公平锁运行 + * @ClassName: ReentrantLockNonFairTest + * @Date: 2022/1/28 13:48 + * @author hz21056617 + * @version V1.0 + */ +@Data +@AllArgsConstructor +public class ReentrantLockFairTest extends Thread { + static final Lock lock = new ReentrantLock(true); + private Integer number; + private CountDownLatch countDownLatch; + private Integer methodNumber; + + /** + * When an object implementing interface Runnable is used + * to create a thread, starting the thread causes the object's + * run method to be called in that separately executing + * thread. + *

+ * The general contract of the method run is that it may + * take any action whatsoever. + * + * @see Thread#run() + */ + @SneakyThrows + @Override + public void run() { + if (0 == methodNumber) { + lockTest(); + } else if (1 == methodNumber) { + tryLockTest(); + } + } + + /** + * lock方法测试 + * @param + * @return void + * @Date 2022/1/28 15:14 + * @Author hz21056617 + * @Version V1.0 + */ + @SneakyThrows + public void lockTest() { + try { + lock.lock(); + System.out.println(number); + Thread.sleep(500); + } + finally { + countDownLatch.countDown(); + lock.unlock(); + } + } + + /** + * tryLock方法测试 + * @param + * @return void + * @Date 2022/1/28 15:13 + * @Author hz21056617 + * @Version V1.0 + */ + @SneakyThrows + public void tryLockTest() { + if (lock.tryLock()) { + try { + System.out.println(number); + Thread.sleep(500); + } + finally { + countDownLatch.countDown(); + lock.unlock(); + } + } else { + countDownLatch.countDown(); + System.out.println("上个线程还没有执行完呢"); + } + } +} diff --git a/concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockNonFairTest.java b/concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockNonFairTest.java new file mode 100644 index 0000000..6e47dfe --- /dev/null +++ b/concurrent/src/main/java/concurrent/reentrantlock/ReentrantLockNonFairTest.java @@ -0,0 +1,91 @@ +package concurrent.reentrantlock; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.SneakyThrows; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * ReentrantLock 非公平锁依次运行 + * @ClassName: ReentrantLockNonFairTest + * @Date: 2022/1/28 13:48 + * @author hz21056617 + * @version V1.0 + */ +@Data +@AllArgsConstructor +public class ReentrantLockNonFairTest extends Thread { + static final Lock lock = new ReentrantLock(); + private Integer number; + private CountDownLatch countDownLatch; + private Integer methodNumber; + + /** + * When an object implementing interface Runnable is used + * to create a thread, starting the thread causes the object's + * run method to be called in that separately executing + * thread. + *

+ * The general contract of the method run is that it may + * take any action whatsoever. + * + * @see Thread#run() + */ + @Override + public void run() { + if (0 == methodNumber) { + lockTest(); + } else if (1 == methodNumber) { + tryLockTest(); + } + } + + /** + * lock方法测试 + * @param + * @return void + * @Date 2022/1/28 15:14 + * @Author hz21056617 + * @Version V1.0 + */ + @SneakyThrows + public void lockTest() { + try { + lock.lock(); + System.out.println(number); + Thread.sleep(500); + } + finally { + countDownLatch.countDown(); + lock.unlock(); + } + } + + /** + * tryLock方法测试 + * @param + * @return void + * @Date 2022/1/28 15:13 + * @Author hz21056617 + * @Version V1.0 + */ + @SneakyThrows + public void tryLockTest() { + if (lock.tryLock()) { + try { + System.out.println(number); + Thread.sleep(500); + } + finally { + countDownLatch.countDown(); + lock.unlock(); + } + } else { + countDownLatch.countDown(); + System.out.println("上个线程还没有执行完呢"); + } + } +} diff --git a/concurrent/src/test/java/concurrent/queue/QueueTest.java b/concurrent/src/test/java/concurrent/queue/QueueTest.java new file mode 100644 index 0000000..6a7e21a --- /dev/null +++ b/concurrent/src/test/java/concurrent/queue/QueueTest.java @@ -0,0 +1,40 @@ +package concurrent.queue; + + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.ArrayBlockingQueue; + +public class QueueTest { + @Test + public void ArrayBlockingQueueOffer() throws InterruptedException { + ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(10); + for (Integer i = 0; i < 100; i++){ + new BlockingQueueTestWork(arrayBlockingQueue, 1).start(); + } + Thread.sleep(10000000); + } + + @Test + public void ArrayBlockingQueueAdd() throws InterruptedException { + ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(10); + for (Integer i = 0; i < 100; i++){ + new BlockingQueueTestWork(arrayBlockingQueue, 2).start(); + } + Thread.sleep(10000000); + } + + @Test + public void ArrayBlockingQueuePut() throws InterruptedException { + ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(10); + for (Integer i = 0; i < 100; i++){ + new BlockingQueueTestWork(arrayBlockingQueue, 3).start(); + } + Thread.sleep(10000000); + } + + @Test + public void reentrantLockNonFairTestTryLock() throws InterruptedException { + } + +} diff --git a/concurrent/src/test/java/concurrent/reentrantlock/ReentrantLockTest.java b/concurrent/src/test/java/concurrent/reentrantlock/ReentrantLockTest.java new file mode 100644 index 0000000..9d77c55 --- /dev/null +++ b/concurrent/src/test/java/concurrent/reentrantlock/ReentrantLockTest.java @@ -0,0 +1,45 @@ +package concurrent.reentrantlock; + + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CountDownLatch; + +public class ReentrantLockTest { + @Test + public void reentrantLockFairTestLock() throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(100); + for (Integer i = 0; i < 100; i++){ + new ReentrantLockFairTest(i, countDownLatch, 0).start(); + } + countDownLatch.await(); + } + + @Test + public void reentrantLockFairTestTryLock() throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(100); + for (Integer i = 0; i < 100; i++){ + new ReentrantLockFairTest(i, countDownLatch, 1).start(); + } + countDownLatch.await(); + } + + @Test + public void reentrantLockNonFairTestLock() throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(100); + for (Integer i = 0; i < 100; i++){ + new ReentrantLockNonFairTest(i, countDownLatch, 0).start(); + } + countDownLatch.await(); + } + + @Test + public void reentrantLockNonFairTestTryLock() throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(100); + for (Integer i = 0; i < 100; i++){ + new ReentrantLockNonFairTest(i, countDownLatch, 1).start(); + } + countDownLatch.await(); + } + +} diff --git a/elasticsearch/pom.xml b/elasticsearch/pom.xml new file mode 100644 index 0000000..8991654 --- /dev/null +++ b/elasticsearch/pom.xml @@ -0,0 +1,19 @@ + + + + JavaBasiceDemo + com.wyl.example + 1.0-SNAPSHOT + + 4.0.0 + + elasticsearch + + + 8 + 8 + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 95484eb..4ac5b20 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,8 @@ 1.0-SNAPSHOT dynamic-proxy + elasticsearch + concurrent pom @@ -24,5 +26,11 @@ - + + + org.projectlombok + lombok + 1.18.20 + + \ No newline at end of file