yangyoupeng

锁的多线程测试

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