-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectLock.java
146 lines (134 loc) · 3.42 KB
/
ObjectLock.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.dushyant.concurrent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author [email protected]
*/
@SuppressWarnings({"unused", "RedundantSuppression"})
public class ObjectLock<T> {
//<editor-fold desc="Main method with examples">
/*
public static void main(String[] args) {
final Thread runnable1 = new Thread(() -> {
ObjectLock.executeInNamedLock("110", () -> {
try {
System.out.println("running 110");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
});
final Thread runnable2 = new Thread(() -> {
ObjectLock.executeInNamedLock("111", () -> {
try {
System.out.println("running 111");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
});
final Thread runnable3 = new Thread(() -> {
ObjectLock.executeInNamedLock("110", () -> {
try {
System.out.println("running 110 2");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
});
runnable1.start();
runnable2.start();
runnable3.start();
}
*/
//</editor-fold>
private final Lock lock = new ReentrantLock();
private final Map<T, Condition> conditions = new HashMap<>();
private volatile static ObjectLock<?> INSTANCE;
private ObjectLock() {
}
/**
* @return thread friendly instance one per jvm session
*/
public static <T> ObjectLock<T> getInstance() {
if (INSTANCE == null) {
synchronized (ObjectLock.class) {
if (INSTANCE == null) {
INSTANCE = new ObjectLock<T>();
}
}
}
//noinspection unchecked
return (ObjectLock<T>) INSTANCE;
}
/**
* @param t T
*/
public void lock(T t) {
lock.lock();
try {
while (conditions.containsKey(t)) {
conditions.get(t).awaitUninterruptibly();
}
conditions.put(t, lock.newCondition());
} finally {
lock.unlock();
}
}
/**
* @param userObject to unlock
*/
public void unlock(T userObject) {
lock.lock();
try {
Condition condition = conditions.get(userObject);
if (condition == null) {
throw new IllegalStateException();
}
conditions.remove(userObject);
condition.signalAll();
} finally {
lock.unlock();
}
}
/**
* @param lockName lock name
* @param runnable runnable
*/
public static void executeInNamedLock(String lockName, Runnable runnable) {
synchronized (lockName.intern()) {
runnable.run();
}
}
/**
* @param t t
* @param time time
* @param unit unit
* @return false if couldn't hold lock for given time
* @throws InterruptedException if interrupted
*/
public boolean tryLock(T t, long time, TimeUnit unit) throws InterruptedException {
boolean hold = true;
lock.lock();
try {
while (conditions.containsKey(t)) {
final boolean await = conditions.get(t).await(time, unit);
if (!await) {
hold = false;
break;
}
}
conditions.put(t, lock.newCondition());
} finally {
lock.unlock();
}
return hold;
}
}