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
Show 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;
*
* @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
();
}
...
...
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