- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestForStampedLock.java
More file actions
Latest commit
99 lines (83 loc) · 2.99 KB
/
Copy pathTestForStampedLock.java
File metadata and controls
99 lines (83 loc) · 2.99 KB
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
importjava.util.concurrent.TimeUnit;
importcom.hui.stamped.pac.StampedLock;
publicclassTestForStampedLock {
/**
* @param args
*/
publicstaticvoidmain(String[] args) throwsInterruptedException{
// TODO Auto-generated method stub
StampedLocklock = newStampedLock();
newThread(newWriteThread(lock)).start();
// new Thread(new ReadThreadTry(lock)).start();
Thread.sleep(200);
newThread(newReadThreadTry(lock)).start();
Thread.sleep(100);
// new Thread(new ReadThreadTry(lock)).start();
for( inti = 0; i < 6; ++i)
newThread(newReadThread(lock)).start();
Thread.sleep(300);
for( inti = 0; i < 6; ++i)
newThread(newWriteThreadLast(lock)).start();
}
privatestaticclassWriteThreadimplementsRunnable{
privateStampedLocklock;
publicWriteThread(StampedLocklock){
this.lock = lock;
}
publicvoidrun(){
longwriteLong = lock.writeLock();
System.out.println(Thread.currentThread().getName() + " WRITE");
try {
Thread.sleep(1000);
} catch (InterruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lock.unlockWrite(writeLong);
}
}
privatestaticclassReadThreadTryimplementsRunnable{
privateStampedLocklock;
publicReadThreadTry(StampedLocklock){
this.lock = lock;
}
publicvoidrun(){
longreadLong = 0;
try {
readLong = lock.tryReadLock(600, TimeUnit.MILLISECONDS);
if(readLong > 0)
System.out.println(Thread.currentThread().getName() + " READ LOCK");
else
System.out.println(Thread.currentThread().getName() + " READ noLOCK");
} catch (InterruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(readLong>0)
lock.unlockRead(readLong);
}
}
}
privatestaticclassReadThreadimplementsRunnable{
privateStampedLocklock;
publicReadThread(StampedLocklock){
this.lock = lock;
}
publicvoidrun(){
longreadLong = lock.readLock();
System.out.println(Thread.currentThread().getName() + " READ");
lock.unlockRead(readLong);
}
}
privatestaticclassWriteThreadLastimplementsRunnable{
privateStampedLocklock;
publicWriteThreadLast(StampedLocklock){
this.lock = lock;
}
publicvoidrun(){
longwriteLong = lock.writeLock();
System.out.println(Thread.currentThread().getName() + " WRITE");
lock.unlockWrite(writeLong);
}
}
}