Skip to content

Commit eedd566

Browse files
committed
Add TriResult class for handling three-value results and implement main method in PWRReentrantReadWriteLock for lock testing
1 parent ff99bf1 commit eedd566

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.pwrlabs.util.encoders;
2+
3+
public class TriResult<T, V, Z> {
4+
private final T first;
5+
private final V second;
6+
private final Z third;
7+
8+
public TriResult(T first, V second, Z third) {
9+
this.first = first;
10+
this.second = second;
11+
this.third = third;
12+
}
13+
14+
public T getFirst() {
15+
return first;
16+
}
17+
18+
public V getSecond() {
19+
return second;
20+
}
21+
22+
public Z getThird() {
23+
return third;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "TriResult{" +
29+
"first=" + first +
30+
", second=" + second +
31+
", third=" + third +
32+
'}';
33+
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (this == obj) return true;
38+
if (obj == null || getClass() != obj.getClass()) return false;
39+
40+
TriResult<?, ?, ?> that = (TriResult<?, ?, ?>) obj;
41+
if (first != null ? !first.equals(that.first) : that.first != null) return false;
42+
if (second != null ? !second.equals(that.second) : that.second != null) return false;
43+
return third != null ? third.equals(that.third) : that.third == null;
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
int result = first != null ? first.hashCode() : 0;
49+
result = 31 * result + (second != null ? second.hashCode() : 0);
50+
result = 31 * result + (third != null ? third.hashCode() : 0);
51+
return result;
52+
}
53+
}

src/main/java/io/pwrlabs/utils/PWRReentrantReadWriteLock.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,25 @@ private void setWriteLockTime(long time) {
242242
}
243243
//endregion
244244

245+
public static void main(String[] args) {
246+
PWRReentrantReadWriteLock lock = new PWRReentrantReadWriteLock();
247+
lock.acquireReadLock();
248+
System.out.println("Read lock acquired");
249+
lock.releaseReadLock();
250+
System.out.println("Read lock released");
251+
252+
lock.acquireWriteLock();
253+
System.out.println("Write lock acquired");
254+
lock.releaseWriteLock();
255+
System.out.println("Write lock released");
256+
257+
//Dead lock test
258+
lock.acquireWriteLock();
259+
lock.acquireReadLock();
260+
System.out.println("No dead lock");
261+
262+
lock.releaseReadLock();
263+
lock.releaseWriteLock();
264+
}
265+
245266
}

0 commit comments

Comments
 (0)