[Feature] add for new
This commit is contained in:
@@ -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<String> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<DelayElem> delayQueue;
|
||||
|
||||
private WriteThread(DelayQueue<DelayElem> 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<DelayElem> delayQueue;
|
||||
|
||||
private ReadThread(DelayQueue<DelayElem> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于延迟队列内部比较排序
|
||||
* <p>
|
||||
* 当前时间的延迟时间 - 比较对象的延迟时间
|
||||
*
|
||||
* @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<DelayElem> delayQueue = new DelayQueue<>();
|
||||
|
||||
new WriteThread(delayQueue).start();
|
||||
new ReadThread(delayQueue).start();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
Reference in New Issue
Block a user