-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyLockStack.java
More file actions
56 lines (48 loc) · 1010 Bytes
/
Copy pathMyLockStack.java
File metadata and controls
56 lines (48 loc) · 1010 Bytes
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
package java_base;
import java.util.concurrent.locks.ReentrantLock;
public class MyLockStack {
private char[] ca = new char[10];
private int point = 0;
private final ReentrantLock lock = new ReentrantLock();
public int getPoint() {
return point;
}
public char pop() {
lock.lock();
char ch = '0';
try {
if(this.getPoint() > 0) {
point --;
ch = ca[point];
}
} catch (Exception e) {
// TODO: handle exception
}finally {
lock.unlock();
return ch;
}
}
public void push(char c) {
lock.lock();
try {
if(this.getPoint() < ca.length) {
ca[point] = c;
point ++;
}
} catch (Exception e) {
// TODO: handle exception
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyLockStack stack = new MyLockStack();
stack.push('A');
stack.push('B');
stack.push('C');
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}