java 使用ConcurrentHashMap和计数器实现锁
在某些场景下,我们想让线程根据某些业务数据进行排队,简单代码如下:
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
60
61
62
63
64
65
66
67
|
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; public class TestServiceImpl { private static ConcurrentHashMap<Long, LockObj> lockMap = new ConcurrentHashMap<Long, LockObj>( 40 ); public void test(Long userId){ LockObj lock = tryLock(userId); synchronized (lock) { try { //处理业务 } finally { unLock(lock); } } } private LockObj tryLock(Long key) { LockObj curVal = new LockObj(key); LockObj preVal = lockMap.putIfAbsent(key, curVal); if ( null == preVal) { curVal.inc(); return curVal; } else { preVal.inc(); } return preVal; } private void unLock(LockObj lock){ if (lock.dec() <= 0 ){ lockMap.remove(lock.getKey()); } } public class LockObj { private long key = 0 ; private AtomicInteger count = new AtomicInteger( 0 ); public LockObj( long key){ this .key = key; } public int inc(){ return count.incrementAndGet(); } public int dec(){ return count.decrementAndGet(); } public long getKey(){ return key; } @Override public String toString() { return "LockObj [key=" + key + ", count=" + count + "]" ; } } } |
按照userId来排队,如果每个线程处理数据后不释放锁的话,那么可以不利用计数器。但是加了释放锁的操作,则必须加上计算器。因为当线程把锁释放掉后,还没来得及退出synchronized 代码块时,另外一个线程调用了tryLock方法,那该线程将拿到另外一个对象的锁,导致利用synchronized 关键字进行userId排队失败。
也可以利用guava的API来实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import com.google.common.collect.Interner; import com.google.common.collect.Interners; public class TestServiceImpl { Interner<String> pool = Interners.newWeakInterner(); public void test(Long userId) throws OspException { synchronized ( pool.intern(String.valueOf(userId))){ //处理业务操作 } } } |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!