-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConcurrentBST.java
More file actions
169 lines (138 loc) · 4.11 KB
/
ConcurrentBST.java
File metadata and controls
169 lines (138 loc) · 4.11 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.*;
import java.util.concurrent.locks.*;
public class ConcurrentBST<T extends Comparable<T>>
{
static class StartSentinel {
public StartSentinel() {
System.out.println("Starting main...");
}
}
static class EndSentinel {
public EndSentinel() {
System.out.println("Ending main...");
}
}
private ReadWriteLock mx;
private Node<T> root;
private static Random rand = new Random();
class Node<T extends Comparable<T>> {
private T val;
private Node<T> left;
private Node<T> right;
public Node() {
val = null;
left = null;
right = null;
}
public Node(T initVal) {
this();
val = initVal;
}
public void insert(T insertVal) {
if (val == null) {
val = insertVal;
return;
}
if (val.compareTo(insertVal) <= 0) {
if (right == null) {
right = new Node<T>(insertVal);
} else {
right.insert(insertVal);
}
} else if (left == null) {
left = new Node<T>(insertVal);
} else {
left.insert(insertVal);
}
}
public int height() {
if (val == null) {
return 0;
} else if (left == null && right == null) {
return 1;
} else if (left == null) {
return 1 + right.height();
} else if (right == null) {
return 1 + left.height();
} else {
return 1 + Math.max(left.height(), right.height());
}
}
public void inOrder() {
if (left != null) {
left.inOrder();
}
if (val != null) {
System.out.println(val);
}
if (right != null) {
right.inOrder();
}
}
}
public ConcurrentBST() {
root = new Node<T>();
mx = new ReentrantReadWriteLock();
}
public void insert(T insertVal) {
mx.writeLock().lock();
root.insert(insertVal);
mx.writeLock().unlock();
}
public int height() {
mx.readLock().lock();
int height = root.height();
mx.readLock().unlock();
return height;
}
public void inOrder() {
mx.readLock().lock();
root.inOrder();
mx.readLock().unlock();
}
static class TreeRW implements Runnable {
ConcurrentBST<Integer> t;
public TreeRW(ConcurrentBST<Integer> t) {
this.t = t;
assert t != null;
}
public void run() {
for (int i = 0; i < 1000; i++) {
t.insert(rand.nextInt(20001));
}
}
}
public static void main(String[] args) {
StartSentinel startobj = new StartSentinel();
int numThreads = 1;
if (args.length == 0) {
System.err.println("Usage: java ConcurrentBST [num of threads]");
System.exit(1);
} else {
try {
numThreads = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Usage: java ConcurrentBST [num of threads]");
System.exit(1);
}
}
ConcurrentBST<Integer> t = new ConcurrentBST<>();
List<Thread> threadPool = new ArrayList<>();
for (int i = 0; i < numThreads; i++) {
TreeRW rw = new TreeRW(t);
threadPool.add(new Thread(rw));
}
for (Thread th : threadPool) {
th.start();
}
for (Thread th : threadPool) {
try {
th.join();
} catch (InterruptedException e) {
System.err.println("Error encountered when trying to join");
}
}
t.inOrder();
EndSentinel endobj = new EndSentinel();
}
}