Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
27ffc97d
Commit
27ffc97d
authored
Apr 30, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改代码
parent
8bf67b32
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
42 deletions
+38
-42
CacheLock.java
...-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
+38
-42
No files found.
yzg-util-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
View file @
27ffc97d
...
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
...
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
*
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
CacheLock
implements
Runnable
{
public
class
CacheLock
<
T
,
M
>
implements
Runnable
{
/**
/**
* 是否执行标记
* 是否执行标记
...
@@ -20,7 +20,7 @@ public class CacheLock implements Runnable {
...
@@ -20,7 +20,7 @@ public class CacheLock implements Runnable {
/**
/**
* 缓存对象
* 缓存对象
*/
*/
private
final
Cache
cache
;
private
final
Cache
<
T
,
M
>
cache
;
/**
/**
* Redis 锁定时间(豪秒)
* Redis 锁定时间(豪秒)
...
@@ -34,7 +34,7 @@ public class CacheLock implements Runnable {
...
@@ -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 {
...
@@ -52,39 +52,39 @@ public class CacheLock implements Runnable {
/**
/**
* 构造函数
* 构造函数
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @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
);
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
null
);
}
}
/**
/**
* 构造函数
* 构造函数
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param func
* @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
);
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
}
}
/**
/**
* 构造函数
* 构造函数
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param funcWait
等待执行
* @param funcWait
等待期间执行的函数
* @param func
* @param func
执行函数
*/
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
funcWait
,
Runnable
func
)
{
public
CacheLock
(
Cache
<
T
,
M
>
cache
,
int
lockTime
,
int
waitUnitTime
,
T
key
,
Runnable
funcWait
,
Runnable
func
)
{
this
.
cache
=
cache
;
this
.
cache
=
cache
;
this
.
lockTime
=
lockTime
;
this
.
lockTime
=
lockTime
;
this
.
waitUnitTime
=
waitUnitTime
;
this
.
waitUnitTime
=
waitUnitTime
;
...
@@ -96,7 +96,7 @@ public class CacheLock implements Runnable {
...
@@ -96,7 +96,7 @@ public class CacheLock implements Runnable {
/**
/**
* 等待次数
* 等待次数
*
*
* @return
* @return
等待次数
*/
*/
public
int
getWaitCount
()
{
public
int
getWaitCount
()
{
return
waitCount
;
return
waitCount
;
...
@@ -104,6 +104,8 @@ public class CacheLock implements Runnable {
...
@@ -104,6 +104,8 @@ public class CacheLock implements Runnable {
/**
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param func 运行函数
*/
*/
public
void
run
(
Runnable
func
)
{
public
void
run
(
Runnable
func
)
{
this
.
func
=
func
;
this
.
func
=
func
;
...
@@ -119,15 +121,9 @@ public class CacheLock implements Runnable {
...
@@ -119,15 +121,9 @@ public class CacheLock implements Runnable {
return
;
return
;
}
}
// 需要运行的函数
// 需要运行的函数
Runnable
runnable
=
new
Runnable
()
{
@Override
public
void
run
()
{
funcRun
();
}
};
do
{
do
{
// 开启唯一性锁,防止多人运行同一关键字的函数
// 开启唯一性锁,防止多人运行同一关键字的函数
cache
.
tryLockAndRun
(
key
,
lockTime
,
TimeUnit
.
SECONDS
,
runnable
);
cache
.
tryLockAndRun
(
key
,
lockTime
,
TimeUnit
.
SECONDS
,
this
::
funcRun
);
// 假如没有运行,则等待50毫秒后继续运行
// 假如没有运行,则等待50毫秒后继续运行
if
(!
runFlag
)
{
if
(!
runFlag
)
{
this
.
waitCount
++;
this
.
waitCount
++;
...
@@ -152,26 +148,26 @@ public class CacheLock implements Runnable {
...
@@ -152,26 +148,26 @@ public class CacheLock implements Runnable {
/**
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param func
* @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
);
run
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
}
}
/**
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
*
* @param cache
* @param cache
缓存对象
* @param lockTime
* @param lockTime
锁定时间
* @param waitUnitTime
* @param waitUnitTime
等待单位
* @param key
* @param key
关键字
* @param func
* @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
);
CacheLock
lock
=
new
CacheLock
(
cache
,
lockTime
,
waitUnitTime
,
key
,
funcWait
,
func
);
lock
.
run
();
lock
.
run
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment