diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/ArrayBlockingQueueDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/ArrayBlockingQueueDemo.java new file mode 100644 index 0000000..be0d18b --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/ArrayBlockingQueueDemo.java @@ -0,0 +1,58 @@ +package com.github.houbb.lock.test.lock; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.TimeUnit; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class ArrayBlockingQueueDemo { + + private ArrayBlockingQueue queue = new ArrayBlockingQueue<>(3); + + public void put(final String put) throws InterruptedException { + System.out.println("设置开始"); + TimeUnit.SECONDS.sleep(1); + queue.put(put); + System.out.println("设置完成: " + put); + } + + public void take() throws InterruptedException { + System.out.println("获取开始"); + String take = queue.take(); + System.out.println("获取成功: " + take); + } + + public static void main(String[] args) { + final ArrayBlockingQueueDemo queueTest = new ArrayBlockingQueueDemo(); + // 写入线程 + new Thread(new Runnable() { + @Override + public void run() { + try { + for(int i = 0; i < 3; i++) { + queueTest.put(i+"T"); + } + } 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(); + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/ConcurrentLinkedQueueTest.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/ConcurrentLinkedQueueTest.java new file mode 100644 index 0000000..a29278b --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/ConcurrentLinkedQueueTest.java @@ -0,0 +1,36 @@ +package com.github.houbb.lock.test.lock; + +import org.junit.Test; + +import java.util.concurrent.ConcurrentLinkedQueue; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class ConcurrentLinkedQueueTest { + + @Test + public void helloTest() { + ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue<>(); + + // add() 将指定元素插入此队列的尾部。 + queue.add("add"); + + // offer() 将指定元素插入此队列的尾部。 + queue.offer("offer"); + + // peek() 获取但不移除此队列的头;如果此队列为空,则返回 null + String value = queue.peek(); + System.out.println("PEEK: " + value); + + // poll() 获取并移除此队列的头,如果此队列为空,则返回 null。 + String poll = queue.poll(); + System.out.println("POLL: " + poll); + + // remove() 移除 从队列中移除指定元素的单个实例(如果存在)。 + boolean remove = queue.remove("offer"); + System.out.println("Remove result: " + remove); + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/DelayQueueDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/DelayQueueDemo.java new file mode 100644 index 0000000..ab89a14 --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/DelayQueueDemo.java @@ -0,0 +1,134 @@ +package com.github.houbb.lock.test.lock; + +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +/** + * @author binbin.hou + * @see java.util.concurrent.DelayQueue + * @since 1.0.0 + */ +public class DelayQueueDemo { + + + /** + * 写入线程 + * @author 老马啸西风 + */ + private static class WriteThread extends Thread { + + private final DelayQueue delayQueue; + + private WriteThread(DelayQueue delayQueue) { + this.delayQueue = delayQueue; + } + + @Override + public void run() { + for(int i = 0; i < 3; i++) { + try { + TimeUnit.MILLISECONDS.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + DelayElem element = new DelayElem(1000,i+"test"); + delayQueue.offer(element); + System.out.println(System.currentTimeMillis() + " 放入元素 " + i); + } + } + } + + /** + * 读取线程 + * @author 老马啸西风 + */ + private static class ReadThread extends Thread { + + private final DelayQueue delayQueue; + + private ReadThread(DelayQueue delayQueue) { + this.delayQueue = delayQueue; + } + + @Override + public void run() { + while (true){ + try { + DelayElem element = delayQueue.take(); + System.out.println(System.currentTimeMillis() +" 获取元素:" + element); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + + private static class DelayElem implements Delayed { + + /** + * 延迟时间 + */ + private final long delay; + /** + * 到期时间 + */ + private final long expire; + /** + * 数据 + */ + private final String msg; + + private DelayElem(long delay, String msg) { + this.delay = delay; + this.msg = msg; + //到期时间 = 当前时间+延迟时间 + this.expire = System.currentTimeMillis() + this.delay; + } + + /** + * 需要实现的接口,获得延迟时间 + * + * 用过期时间-当前时间 + * @param unit 时间单位 + * @return 延迟时间 + */ + @Override + public long getDelay(TimeUnit unit) { + return unit.convert(this.expire - System.currentTimeMillis() , TimeUnit.MILLISECONDS); + } + + /** + * 用于延迟队列内部比较排序 + *

+ * 当前时间的延迟时间 - 比较对象的延迟时间 + * + * @param o 比较对象 + * @return 结果 + */ + @Override + public int compareTo(Delayed o) { + return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); + } + + @Override + public String toString() { + return "DelayElem{" + + "delay=" + delay + + ", expire=" + expire + + ", msg='" + msg + '\'' + + '}'; + } + + } + + public static void main(String[] args) throws InterruptedException { + DelayQueue delayQueue = new DelayQueue<>(); + + new WriteThread(delayQueue).start(); + new ReadThread(delayQueue).start(); + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingQueueDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingQueueDemo.java new file mode 100644 index 0000000..4a2420e --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingQueueDemo.java @@ -0,0 +1,57 @@ +package com.github.houbb.lock.test.lock; + +import java.util.concurrent.*; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class LinkedBlockingQueueDemo { + + private BlockingQueue queue = new LinkedBlockingQueue<>(3); + + public void put(final String put) throws InterruptedException { + System.out.println("设置开始"); + TimeUnit.SECONDS.sleep(1); + queue.put(put); + System.out.println("设置完成: " + put); + } + + public void take() throws InterruptedException { + System.out.println("获取开始"); + String take = queue.take(); + System.out.println("获取成功: " + take); + } + + public static void main(String[] args) { + final LinkedBlockingQueueDemo queueTest = new LinkedBlockingQueueDemo(); + // 写入线程 + new Thread(new Runnable() { + @Override + public void run() { + try { + for(int i = 0; i < 3; i++) { + queueTest.put(i+"T"); + } + } 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(); + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/SynchronousQueueDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/SynchronousQueueDemo.java new file mode 100644 index 0000000..6223f71 --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/SynchronousQueueDemo.java @@ -0,0 +1,9 @@ +package com.github.houbb.lock.test.lock; + +/** + * @author binbin.hou + * @since 1.0.0 + * @see java.util.concurrent.SynchronousQueue + */ +public class SynchronousQueueDemo { +}