Commit 27ffc97d authored by yanzg's avatar yanzg

修改代码

parent 8bf67b32
......@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
*
* @author 颜佐光
*/
public class CacheLock implements Runnable {
public class CacheLock<T, M> implements Runnable {
/**
* 是否执行标记
......@@ -20,7 +20,7 @@ public class CacheLock implements Runnable {
/**
* 缓存对象
*/
private final Cache cache;
private final Cache<T, M> cache;
/**
* Redis 锁定时间(豪秒)
......@@ -34,7 +34,7 @@ public class CacheLock implements Runnable {
/**
* 关键字
*/
private final String key;
private final T key;
/**
* 执行函数
*/
......@@ -52,39 +52,39 @@ public class CacheLock implements Runnable {
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param cache 缓存对象
* @param lockTime 锁定时间
* @param waitUnitTime 等待单位
* @param key 关键字
*/
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key) {
public CacheLock(Cache<T, M> cache, int lockTime, int waitUnitTime, T key) {
this(cache, lockTime, waitUnitTime, key, null, null);
}
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param func
* @param cache 缓存对象
* @param lockTime 锁定时间
* @param waitUnitTime 等待单位
* @param key 关键字
* @param func 执行函数
*/
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key, Runnable func) {
public CacheLock(Cache<T, M> cache, int lockTime, int waitUnitTime, T key, Runnable func) {
this(cache, lockTime, waitUnitTime, key, null, func);
}
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param funcWait  等待执行
* @param func
*/
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key, Runnable funcWait, Runnable func) {
* @param cache 缓存对象
* @param lockTime 锁定时间
* @param waitUnitTime 等待单位
* @param key 关键字
* @param funcWait 等待期间执行的函数
* @param func 执行函数
*/
public CacheLock(Cache<T, M> cache, int lockTime, int waitUnitTime, T key, Runnable funcWait, Runnable func) {
this.cache = cache;
this.lockTime = lockTime;
this.waitUnitTime = waitUnitTime;
......@@ -96,7 +96,7 @@ public class CacheLock implements Runnable {
/**
* 等待次数
*
* @return
* @return 等待次数
*/
public int getWaitCount() {
return waitCount;
......@@ -104,6 +104,8 @@ public class CacheLock implements Runnable {
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param func 运行函数
*/
public void run(Runnable func) {
this.func = func;
......@@ -119,15 +121,9 @@ public class CacheLock implements Runnable {
return;
}
// 需要运行的函数
Runnable runnable = new Runnable() {
@Override
public void run() {
funcRun();
}
};
do {
// 开启唯一性锁,防止多人运行同一关键字的函数
cache.tryLockAndRun(key, lockTime, TimeUnit.SECONDS, runnable);
cache.tryLockAndRun(key, lockTime, TimeUnit.SECONDS, this::funcRun);
// 假如没有运行,则等待50毫秒后继续运行
if (!runFlag) {
this.waitCount++;
......@@ -152,26 +148,26 @@ public class CacheLock implements Runnable {
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param func
* @param cache 缓存对象
* @param lockTime 锁定时间
* @param waitUnitTime 等待单位
* @param key 关键字
* @param func 执行函数
*/
public static void run(Cache cache, int lockTime, int waitUnitTime, String key, Runnable func) {
public static <T, M> void run(Cache<T, M> cache, int lockTime, int waitUnitTime, T key, Runnable func) {
run(cache, lockTime, waitUnitTime, key, null, func);
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param func
* @param cache 缓存对象
* @param lockTime 锁定时间
* @param waitUnitTime 等待单位
* @param key 关键字
* @param func 执行函数
*/
public static void run(Cache cache, int lockTime, int waitUnitTime, String key, Runnable funcWait, Runnable func) {
public static <T, M> void run(Cache<T, M> cache, int lockTime, int waitUnitTime, T key, Runnable funcWait, Runnable func) {
CacheLock lock = new CacheLock(cache, lockTime, waitUnitTime, key, funcWait, func);
lock.run();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment