Toggle navigation
Toggle navigation
This project
Loading...
Sign in
yangyoupeng
/
microservice
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
yangyoupeng
2016-11-22 13:24:00 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
1209c806e6b70d0a51cc94714ea1382e130e62dc
1209c806
1 parent
b7c6fbf2
锁的多线程测试
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
110 additions
and
1 deletions
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/lock/ExcludeLock.java
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/token/MultiSubmitToken.java
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/token/Token.java
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/lock/TestThread2.java
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/lock/ExcludeLock.java
View file @
1209c80
...
...
@@ -85,12 +85,14 @@ public class ExcludeLock {
public
boolean
acquireLock
()
throws
InterruptedException
{
int
timeout
=
acquiryTimeoutInMillis
;
BoundValueOperations
<
String
,
String
>
valuOper
=
redisTemplate
.
boundValueOps
(
lockKeyPath
);
while
(
timeout
>=
0
)
{
final
Lock
newLock
=
asLock
(
System
.
currentTimeMillis
()
+
lockExpiryInMillis
);
Boolean
result
=
valuOper
.
setIfAbsent
(
newLock
.
toString
());
valuOper
.
expire
(
lockExpiryInMillis
,
TimeUnit
.
MILLISECONDS
);
logger
.
debug
(
"new lock [{}] setIfAbsent result is [{}]"
,
newLock
,
result
);
// final String currentValueStr1 = valuOper.get();
// System.out.println(currentValueStr1);
if
(
result
)
{
this
.
lock
=
newLock
;
return
true
;
...
...
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/token/MultiSubmitToken.java
View file @
1209c80
...
...
@@ -38,6 +38,19 @@ public class MultiSubmitToken {
* @param submitTimeMills
* @return boolean<BR>
*/
public
boolean
addSubmitToken
(
String
submitID
,
Long
submitTimeMills
,
int
expiryInMillis
){
return
token
.
addToken
(
submitID
,
submitTimeMills
,
expiryInMillis
);
}
/**
* Method name: addSubmitToken <BR>
* Description: 在提交的时候,只有在transactionID的提交信息已经不存在的情况下才能返回true <BR>
* Remark: <BR>
* @param transactionID
* @param submitTimeMills
* @return boolean<BR>
*/
public
boolean
addSubmitToken
(
String
submitID
,
Long
submitTimeMills
){
return
token
.
addToken
(
submitID
,
submitTimeMills
);
}
...
...
src/microservice-parent/microservice-redis/src/main/java/com/zhaoonline/redis/token/Token.java
View file @
1209c80
...
...
@@ -52,6 +52,13 @@ public class Token<T> {
this
.
tokenNameSpace
=
tokenNameSpace
;
}
public
boolean
addToken
(
String
key
,
T
token
,
int
expiryInMillis
){
BoundValueOperations
<
String
,
T
>
valuOper
=
redisTemplate
.
boundValueOps
(
buildTokenKeyWithNameSpace
(
key
));
boolean
result
=
valuOper
.
setIfAbsent
(
token
);
valuOper
.
expire
(
expiryInMillis
,
TimeUnit
.
MILLISECONDS
);
return
result
;
}
public
boolean
addToken
(
String
key
,
T
token
){
BoundValueOperations
<
String
,
T
>
valuOper
=
redisTemplate
.
boundValueOps
(
buildTokenKeyWithNameSpace
(
key
));
boolean
result
=
valuOper
.
setIfAbsent
(
token
);
...
...
src/microservice-parent/microservice-redis/src/test/java/com/zhaoonline/redis/lock/TestThread2.java
0 → 100644
View file @
1209c80
package
com
.
zhaoonline
.
redis
.
lock
;
import
java.util.concurrent.CountDownLatch
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.data.redis.connection.jedis.JedisConnectionFactory
;
import
com.zhaoonline.redis.lock.TestThread.RunThread
;
import
com.zhaoonline.redis.template.RedisTemplateFactory
;
import
com.zhaoonline.redis.transaction.TransactionValueOperation
;
public
class
TestThread2
{
private
static
String
testLock
=
"testLock"
;
RedisTemplateFactory
factory
=
null
;
JedisConnectionFactory
connectionFactory
=
null
;
@Before
public
void
prepare
(){
connectionFactory
=
createSimpleFactory
();
factory
=
new
RedisTemplateFactory
();
factory
.
setConnectionFactory
(
connectionFactory
);
}
@Test
public
void
testRun
(){
ExcludeLock
lock
=
new
ExcludeLock
(
factory
,
testLock
);
int
num
=
10
;
CountDownLatch
latch
=
new
CountDownLatch
(
num
);
for
(
int
i
=
0
;
i
<
num
;
i
++){
RunThread
t
=
new
RunThread
(
latch
,
i
,
factory
,
lock
);
t
.
start
();
}
try
{
latch
.
await
();
}
catch
(
InterruptedException
e
)
{
}
}
static
class
RunThread
extends
Thread
{
CountDownLatch
latch
;
RedisTemplateFactory
factory
;
int
id
;
ExcludeLock
lock
=
null
;
public
RunThread
(
CountDownLatch
latch
,
int
id
,
RedisTemplateFactory
factory
,
ExcludeLock
lock
){
this
.
latch
=
latch
;
this
.
factory
=
factory
;
this
.
id
=
id
;
this
.
lock
=
lock
;
}
@Override
public
void
run
()
{
try
{
boolean
result
=
lock
.
acquireLock
();
System
.
out
.
println
(
"thread "
+
id
+
" acquireLock result:"
+
result
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
latch
.
countDown
();
super
.
run
();
}
}
private
JedisConnectionFactory
createSimpleFactory
(){
JedisConnectionFactory
factory
=
new
JedisConnectionFactory
();
factory
.
setHostName
(
"192.168.0.188"
);
factory
.
setPort
(
6377
);
factory
.
setUsePool
(
true
);
factory
.
afterPropertiesSet
();
return
factory
;
}
@After
public
void
after
(){
connectionFactory
.
destroy
();
}
}
Please
register
or
login
to post a comment