-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathRedisReentrantLockTemplateTest.java
59 lines (54 loc) · 2.39 KB
/
RedisReentrantLockTemplateTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.distributed.lock.redis;
import com.distributed.lock.Callback;
import com.distributed.lock.zk.ZkDistributedLockTemplate;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.Test;
import redis.clients.jedis.JedisPool;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
/**
* Created by [email protected] on 2016/2/24.
*/
public class RedisReentrantLockTemplateTest {
@Test
public void testTry() throws InterruptedException {
JedisPool jp=new JedisPool("127.0.0.1",6379);
final RedisDistributedLockTemplate template=new RedisDistributedLockTemplate(jp);
int size=100;
final CountDownLatch startCountDownLatch = new CountDownLatch(1);
final CountDownLatch endDownLatch=new CountDownLatch(size);
for (int i =0;i<size;i++){
new Thread() {
public void run() {
try {
startCountDownLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
final int sleepTime=ThreadLocalRandom.current().nextInt(5)*1000;
template.execute("test",5000, new Callback() {
public Object onGetLock() throws InterruptedException {
System.out.println(Thread.currentThread().getName() + ":getLock");
Thread.currentThread().sleep(sleepTime);
System.out.println(Thread.currentThread().getName() + ":sleeped:"+sleepTime);
endDownLatch.countDown();
return null;
}
public Object onTimeout() throws InterruptedException {
System.out.println(Thread.currentThread().getName() + ":timeout");
endDownLatch.countDown();
return null;
}
});
}
}.start();
}
startCountDownLatch.countDown();
endDownLatch.await();
}
}