-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLock.java
More file actions
33 lines (30 loc) · 893 Bytes
/
Copy pathTestLock.java
File metadata and controls
33 lines (30 loc) · 893 Bytes
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
package com.caozj.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.caozj.service.CacheService;
public class TestLock {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/SpringContext.xml");
final CacheService cs = (CacheService) context.getBean("redisByteService");
final String key = "key";
int count = 10000;
for (int i = 0; i < count; i++) {
final int t = i;
new Thread(new Runnable() {
@Override
public void run() {
boolean lock = cs.tryLock(key);
if (lock) {
System.out.println(t + "抢锁成功");
}
}
}).start();
}
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
cs.releaseLock(key);
}
}