From bce83c6d1e3c2720eee27f5a4abfc81df76c5904 Mon Sep 17 00:00:00 2001 From: houbb <1060732496@qq.com> Date: Sat, 31 Oct 2020 12:53:31 +0800 Subject: [PATCH] [Feature] add for new --- .../houbb/lock/test/lock/CowArraySetDemo.java | 59 +++++++++++++ .../test/lock/PriorityBlockingQueueDemo.java | 86 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 lock-test/src/test/java/com/github/houbb/lock/test/lock/CowArraySetDemo.java create mode 100644 lock-test/src/test/java/com/github/houbb/lock/test/lock/PriorityBlockingQueueDemo.java diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/CowArraySetDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/CowArraySetDemo.java new file mode 100644 index 0000000..e25b700 --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/CowArraySetDemo.java @@ -0,0 +1,59 @@ +package com.github.houbb.lock.test.lock; + +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class CowArraySetDemo { + + /** + * 读线程 + */ + private static class ReadTask implements Runnable { + Set set; + + public ReadTask(Set set) { + this.set = set; + } + + public void run() { + System.out.println(set); + } + } + /** + * 写线程 + */ + private static class WriteTask implements Runnable { + private Set set; + private String value; + + public WriteTask(Set set, String value) { + this.set = set; + this.value = value; + } + + public void run() { + set.remove(value); + } + } + + public static void main(String[] args) { + final int NUM = 5; + Set set = new CopyOnWriteArraySet<>(); + for (int i = 0; i < NUM; i++) { + set.add("main_" + i); + } + ExecutorService executorService = Executors.newFixedThreadPool(NUM); + for (int i = 0; i < NUM; i++) { + executorService.execute(new WriteTask(set, "main_" + i)); + executorService.execute(new ReadTask(set)); + } + executorService.shutdown(); + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/PriorityBlockingQueueDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/PriorityBlockingQueueDemo.java new file mode 100644 index 0000000..873254a --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/PriorityBlockingQueueDemo.java @@ -0,0 +1,86 @@ +package com.github.houbb.lock.test.lock; + +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class PriorityBlockingQueueDemo { + + private static class User implements Comparable { + + private final int order; + + private final String name; + + private User(int order, String name) { + this.order = order; + this.name = name; + } + + @Override + public int compareTo(User o) { + return this.order - o.order; + } + + @Override + public String toString() { + return "User{" + + "order=" + order + + ", name='" + name + '\'' + + '}'; + } + } + + private PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); + + public void put(final User user) throws InterruptedException { + System.out.println("设置开始"); + queue.put(user); + System.out.println("设置完成: " + user); + } + + public void take() throws InterruptedException { + TimeUnit.SECONDS.sleep(1); + System.out.println("获取开始"); + User take = queue.take(); + System.out.println("获取成功: " + take); + } + + public static void main(String[] args) { + final PriorityBlockingQueueDemo queueTest = new PriorityBlockingQueueDemo(); + // 写入线程 + new Thread(new Runnable() { + @Override + public void run() { + try { + for(int i = 0; i < 5; i++) { + int order = ThreadLocalRandom.current().nextInt(10); + User user = new User(order, i+"-user"); + queueTest.put(user); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); + + // 读取线程 + new Thread(new Runnable() { + @Override + public void run() { + try { + while (true) { + queueTest.take(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); + } + +}