-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleLock.java
95 lines (79 loc) · 2.39 KB
/
SimpleLock.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
/*
* SimpleLock
*
* Implements a simple lock by wrapping itself around
* ReentrantLock. We do not implement all Lock
* methods (to simplify the interface).
*
* You must follow the coding standards distributed
* on the class web page.
*
* (C) 2007 Mike Dahlin
*
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.TimeUnit;
public class SimpleLock implements Lock{
ReentrantLock daLock;
//-------------------------------------------------
// Constructor
//-------------------------------------------------
public SimpleLock()
{
daLock = new ReentrantLock();
}
//-------------------------------------------------
// lock() -- acquires the lock
//-------------------------------------------------
public void lock()
{
daLock.lock();
}
//-------------------------------------------------
// lock() -- releases the lock
//-------------------------------------------------
public void unlock()
{
assert(daLock.isHeldByCurrentThread());
daLock.unlock();
}
//-------------------------------------------------
// newCondition() -- Return a new Condition instance
// that is bound to this Lock instance.
//-------------------------------------------------
public Condition newCondition()
{
return daLock.newCondition();
}
//-------------------------------------------------
// lockInterruptibly() -- NOT SUPPORTED
//-------------------------------------------------
public void lockInterruptibly()
{
System.out.println("SimpleLock::lockInterruptibly() not supported");
assert(false);
System.exit(-1);
}
//-------------------------------------------------
// tryLock() -- NOT SUPPORTED
//-------------------------------------------------
public boolean tryLock()
{
System.out.println("SimpleLock::tryLock() not supported");
assert(false);
System.exit(-1);
return false;
}
//-------------------------------------------------
// tryLock() -- NOT SUPPORTED
//-------------------------------------------------
public boolean tryLock(long time, TimeUnit unit)
{
System.out.println("SimpleLock::tryLock() not supported");
assert(false);
System.exit(-1);
return false;
}
}