it = idx.descendingIterator();
68 | while (it.hasNext()) {
69 | Integer i = (Integer) it.next();
70 | ObjectLock lock = locks.get(i.intValue());
71 | Lock tieLock = holder.getTieLock(lock.getClz());
72 | newLocks.add(i.intValue(), tieLock);
73 | }
74 | return newLocks;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/sb-commons/common-lock/src/main/java/org/chinasb/common/lock/ObjectLock.java:
--------------------------------------------------------------------------------
1 | package org.chinasb.common.lock;
2 |
3 | import java.util.concurrent.locks.ReentrantLock;
4 |
5 | /**
6 | * 对象锁
7 | *
8 | *
9 | * 排序顺序说明:
10 | * 1.非实体在前,实体{@link IEntity}在后
11 | * 2.不同类型的锁对象,按类型{@link Class}的hashCode值的大小进行排序
12 | * 3.不同类型的锁对象,当不幸遇到hashCode值相同的情况,用完整类名做字符串排序
13 | * 4.类型相同时,使用{@link ObjectLock#value}
进行排序
14 | * 5.{@link ObjectLock#value}
对于非实体而言,为System.identityHashCode(instance)
15 | * 6.{@link ObjectLock#value}
对于实体而言,为{@link IEntity#getIdentity()}
16 | *
17 | *
18 | * @author zhujuan
19 | */
20 | @SuppressWarnings("rawtypes")
21 | public class ObjectLock extends ReentrantLock implements Comparable {
22 |
23 | private static final long serialVersionUID = 1L;
24 | private static final Class IENTITY_CLASS = IEntity.class;
25 | /**
26 | * 对象锁的类型
27 | */
28 | private final Class clz;
29 | /**
30 | * 对象锁用于排序的元素
31 | */
32 | private final Comparable value;
33 |
34 | /**
35 | * 构造一个对象实例的对象锁
36 | *
37 | * @param object
38 | */
39 | public ObjectLock(Object object) {
40 | this(object, true);
41 | }
42 |
43 | /**
44 | * 构造一个对象实例的对象锁
45 | *
46 | * @param object
47 | * @param fair {@link ReentrantLock#isFair()}
48 | */
49 | public ObjectLock(Object object, boolean fair) {
50 | super(fair);
51 | this.clz = object.getClass();
52 | if (object instanceof IEntity)
53 | this.value = ((IEntity) object).getIdentity();
54 | else {
55 | this.value = new Integer(System.identityHashCode(object));
56 | }
57 | }
58 |
59 | /**
60 | * 加时锁检查(检查当前对象锁与指定对象锁是否可以保证获取顺序)
61 | *
62 | * @param other
63 | * @return
64 | */
65 | @SuppressWarnings("unchecked")
66 | public boolean isTie(ObjectLock other) {
67 | if (this.clz != other.clz) {
68 | return false;
69 | }
70 | if (this.value.compareTo(other.value) == 0) {
71 | return true;
72 | }
73 | return false;
74 | }
75 |
76 | /**
77 | * 获取对象锁的类型
78 | *
79 | * @return
80 | */
81 | public Class getClz() {
82 | return clz;
83 | }
84 |
85 | /**
86 | * 获取对象锁的排序元素
87 | *
88 | * @return
89 | */
90 | public Comparable getValue() {
91 | return value;
92 | }
93 |
94 | /**
95 | * 检查当前对象锁是否是实体的对象锁
96 | *
97 | * @return
98 | */
99 | public boolean isEntity() {
100 | return IENTITY_CLASS.isAssignableFrom(clz);
101 | }
102 |
103 | @SuppressWarnings("unchecked")
104 | @Override
105 | public int compareTo(ObjectLock o) {
106 | if (this.isEntity() && !o.isEntity()) {
107 | return 1;
108 | }
109 | if (!this.isEntity() && o.isEntity()) {
110 | return -1;
111 | }
112 | if (this.clz != o.clz) {
113 | if (this.clz.hashCode() < o.clz.hashCode()) {
114 | return -1;
115 | }
116 | if (this.clz.hashCode() > o.clz.hashCode()) {
117 | return 1;
118 | }
119 | return this.clz.getName().compareTo(o.clz.getName());
120 | }
121 | return this.value.compareTo(o.value);
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/sb-commons/common-lock/src/main/java/org/chinasb/common/lock/ObjectLockHolder.java:
--------------------------------------------------------------------------------
1 | package org.chinasb.common.lock;
2 |
3 | import java.util.concurrent.locks.Lock;
4 | import java.util.concurrent.locks.ReentrantLock;
5 |
6 | import com.google.common.cache.CacheBuilder;
7 | import com.google.common.cache.CacheLoader;
8 | import com.google.common.cache.LoadingCache;
9 |
10 | /**
11 | * 对象锁持有者
12 | *
13 | * @author zhujuan
14 | */
15 | @SuppressWarnings("rawtypes")
16 | public class ObjectLockHolder {
17 | /**
18 | * 所有类的对象锁持有者缓存
19 | */
20 | private final LoadingCache HOLDERS = CacheBuilder.newBuilder().maximumSize(1000)
21 | .build(new CacheLoader() {
22 | @Override
23 | public Holder load(Class clz) {
24 | return new Holder(clz);
25 | }
26 | });
27 |
28 | /**
29 | * 类的对象锁持有者
30 | *
31 | * @author zhujuan
32 | *
33 | */
34 | public class Holder {
35 | /**
36 | * 对象锁持有者的类型
37 | */
38 | @SuppressWarnings("unused")
39 | private final Class clz;
40 | /**
41 | * 加时锁,用于当对象的hash值(或实现自Entity接口的getIdentity())一样时保证锁的获取顺序
42 | */
43 | private final Lock tieLock = new ReentrantLock();
44 | /**
45 | * 实例的对象锁缓存
46 | */
47 | private final LoadingCache