release branch 0.0.4

This commit is contained in:
binbin.hou
2022-12-07 16:40:24 +08:00
parent 7fd29cd1f2
commit 402653393d
50 changed files with 458 additions and 2253 deletions

View File

@@ -16,10 +16,6 @@
<groupId>com.github.houbb</groupId>
<artifactId>lock-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>lock-redis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>

View File

@@ -1,35 +0,0 @@
package com.github.houbb.lock.test.core;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.core.core.LockSpinRe;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class LockSpinReThread implements Runnable {
private final ILock lock = new LockSpinRe();
@Override
public void run() {
System.out.println("first-lock: " + Thread.currentThread().getId());
lock.lock();
System.out.println("second-lock: " + Thread.currentThread().getId());
lock.lock();
lock.unlock();
System.out.println("second-unlock: " + Thread.currentThread().getId());
lock.unlock();
System.out.println("first-unlock: " + Thread.currentThread().getId());
}
public static void main(String[] args) {
final Runnable runnable = new LockSpinReThread();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
}

View File

@@ -1,35 +0,0 @@
package com.github.houbb.lock.test.core;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.core.core.LockSpin;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class LockSpinThread implements Runnable {
private final ILock lock = new LockSpin();
@Override
public void run() {
System.out.println("first-lock: " + Thread.currentThread().getId());
lock.lock();
System.out.println("second-lock: " + Thread.currentThread().getId());
lock.lock();
lock.unlock();
System.out.println("second-unlock: " + Thread.currentThread().getId());
lock.unlock();
System.out.println("first-unlock: " + Thread.currentThread().getId());
}
public static void main(String[] args) {
final Runnable runnable = new LockSpinThread();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
}

View File

@@ -1,39 +0,0 @@
package com.github.houbb.lock.test.core;
import com.github.houbb.heaven.util.util.DateUtil;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.core.core.LockWaitNotify;
import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import java.util.concurrent.TimeUnit;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class LockWaitNotifyThread implements Runnable {
private static final Log log = LogFactory.getLog(LockWaitNotifyThread.class);
private final ILock lock = new LockWaitNotify();
@Override
public void run() {
log.debug("first lock");
lock.lock();
log.info("执行业务逻辑。");
DateUtil.sleep(TimeUnit.SECONDS, 5);
lock.unlock();
log.debug("first unlock");
}
public static void main(String[] args) {
final Runnable runnable = new LockWaitNotifyThread();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
}

View File

@@ -1,45 +0,0 @@
package com.github.houbb.lock.test.core;
import com.github.houbb.heaven.util.util.DateUtil;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.core.core.LockWaitNotify;
import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import java.util.concurrent.TimeUnit;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class LockWaitNotifyThread2 implements Runnable {
private static final Log log = LogFactory.getLog(LockWaitNotifyThread2.class);
private final ILock lock = new LockWaitNotify();
@Override
public void run() {
log.debug("first lock");
lock.lock();
log.debug("second lock");
lock.lock();
log.info("执行业务逻辑。");
DateUtil.sleep(TimeUnit.SECONDS, 5);
log.debug("second unlock");
lock.unlock();
lock.unlock();
log.debug("first unlock");
}
public static void main(String[] args) {
final Runnable runnable = new LockWaitNotifyThread2();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
}

View File

@@ -1,39 +0,0 @@
package com.github.houbb.lock.test.core;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.core.core.LockWaitNotifyRe;
import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class LockWaitNotifyThreadRe implements Runnable {
private static final Log log = LogFactory.getLog(LockWaitNotifyThreadRe.class);
private final ILock lock = new LockWaitNotifyRe();
@Override
public void run() {
log.debug("first lock");
lock.lock();
log.debug("second lock");
lock.lock();
log.debug("second unlock");
lock.unlock();
log.debug("first unlock");
lock.unlock();
}
public static void main(String[] args) {
final Runnable runnable = new LockWaitNotifyThreadRe();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
}

View File

@@ -1,25 +0,0 @@
package com.github.houbb.lock.test;
import java.util.concurrent.Phaser;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class MyPhaser extends Phaser {
@Override
protected boolean onAdvance(int phase, int registeredParties) {
switch (phase) {
case 0 :
System.out.println("上半场完成");
return false;
case 1:
System.out.println("下半场完成");
return false;
default:
return true;
}
}
}

View File

@@ -1,53 +0,0 @@
package com.github.houbb.lock.test;
import java.util.concurrent.Phaser;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class PhaserDemo {
private static class GameRunnable implements Runnable {
private final Phaser phaser;
private GameRunnable(Phaser phaser) {
this.phaser = phaser;
}
@Override
public void run() {
//参加上半场比赛
System.out.println("玩家-"+Thread.currentThread().getName()+":参加上半场比赛");
//执行这个方法的话会等所有的选手都完成了之后再继续下面的方法
phaser.arriveAndAwaitAdvance();
// 下半场
//参加上半场比赛
System.out.println("玩家-"+Thread.currentThread().getName()+":参加下半场比赛");
//执行这个方法的话会等所有的选手都完成了之后再继续下面的方法
phaser.arriveAndAwaitAdvance();
}
}
public static void main(String[] args) {
int nums = 3;
Phaser phaser = new MyPhaser();
//注册一次表示 phaser 维护的线程个数
phaser.register();
for(int i = 0; i < nums; i++) {
phaser.register();
Thread thread = new Thread(new GameRunnable(phaser));
thread.start();
}
//后续阶段主线程就不参加了
phaser.arriveAndDeregister();
}
}

View File

@@ -0,0 +1,28 @@
package com.github.houbb.lock.test.core;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.core.bs.LockBs;
import org.junit.Test;
public class LockBsTest {
@Test
public void helloTest() {
ILock lock = LockBs.newInstance()
.init();
String key = "ddd";
try {
// 加锁
lock.tryLock(key);
System.out.println("业务处理");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 释放锁
lock.unlock(key);
}
}
}

View File

@@ -1,58 +0,0 @@
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();
}
}

View File

@@ -1,36 +0,0 @@
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);
}
}

View File

@@ -1,59 +0,0 @@
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<String> set;
public ReadTask(Set<String> set) {
this.set = set;
}
public void run() {
System.out.println(set);
}
}
/**
* 写线程
*/
private static class WriteTask implements Runnable {
private Set<String> set;
private String value;
public WriteTask(Set<String> 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<String> 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();
}
}

View File

@@ -1,134 +0,0 @@
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();
}
}

View File

@@ -1,57 +0,0 @@
package com.github.houbb.lock.test.lock;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadLocalRandom;
/**
* @author binbin.hou
* @since 1.0.0
*/
public class LinkedBlockingDequeDemo {
private static class Producer implements Runnable{
private BlockingDeque<Integer> queue;
public Producer(BlockingDeque<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
while(true) {
try {
Integer num = ThreadLocalRandom.current().nextInt(100);
queue.put(num);
System.out.println(String.format("%s producer a num %d",Thread.currentThread().getName(),num));
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
private static class Consumer implements Runnable{
private BlockingDeque<Integer> queue;
public Consumer(BlockingDeque<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
while(true) {
try {
System.out.println(String.format("%s consume a num %d",Thread.currentThread().getName(),queue.take()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
BlockingDeque<Integer> queue = new LinkedBlockingDeque<>(100);
new Thread(new Producer(queue),"Producer").start();
new Thread(new Consumer(queue),"Consumer").start();
}
}

View File

@@ -1,57 +0,0 @@
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();
}
}

View File

@@ -1,86 +0,0 @@
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<User> {
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<User> 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();
}
}

View File

@@ -1,9 +0,0 @@
package com.github.houbb.lock.test.lock;
/**
* @author binbin.hou
* @since 1.0.0
* @see java.util.concurrent.SynchronousQueue
*/
public class SynchronousQueueDemo {
}

View File

@@ -1,37 +1,37 @@
package com.github.houbb.lock.test.redis;
import com.github.houbb.lock.api.core.ILock;
import com.github.houbb.lock.api.support.IOperator;
import com.github.houbb.lock.core.bs.LockBs;
import com.github.houbb.lock.redis.support.operator.JedisOperator;
import org.junit.Ignore;
import org.junit.Test;
import redis.clients.jedis.Jedis;
/**
* @author binbin.hou
* @since 0.0.1
*/
@Ignore
public class LockRedisTest {
@Test
public void helloTest() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
IOperator operator = new JedisOperator(jedis);
// 获取锁
ILock lock = LockBs.newInstance(operator).lock();
try {
boolean lockResult = lock.tryLock();
System.out.println(lockResult);
// 业务处理
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
//package com.github.houbb.lock.test.redis;
//
//import com.github.houbb.lock.api.core.ILock;
//import com.github.houbb.lock.api.support.IOperator;
//import com.github.houbb.lock.core.bs.LockBs;
//import com.github.houbb.lock.redis.support.operator.JedisOperator;
//import org.junit.Ignore;
//import org.junit.Test;
//import redis.clients.jedis.Jedis;
//
///**
// * @author binbin.hou
// * @since 0.0.1
// */
//@Ignore
//public class LockRedisTest {
//
// @Test
// public void helloTest() {
// Jedis jedis = new Jedis("127.0.0.1", 6379);
// IOperator operator = new JedisOperator(jedis);
//
// // 获取锁
// ILock lock = LockBs.newInstance(operator).lock();
//
// try {
// boolean lockResult = lock.tryLock();
// System.out.println(lockResult);
// // 业务处理
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// lock.unlock();
// }
// }
//
//}