Skip to content

Commit fcfb01d

Browse files
committed
wrote README w/ output
1 parent d17d1ee commit fcfb01d

File tree

3 files changed

+69
-64
lines changed

3 files changed

+69
-64
lines changed

ArrayQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ArrayQueue<T> {
2020
public ArrayQueue(int capacity) {
2121
headLock = new ReentrantLock();
2222
tailLock = new ReentrantLock();
23-
data = (T[]) new Object[capacity];
23+
data = (T[]) new Object[capacity+1];
2424
head = 0;
2525
tail = 0;
2626
}

Main.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
11
import java.util.*;
22

33
class Main {
4-
static Deque<Integer> stack;
5-
static ArrayQueue<Integer> concurrentStack;
6-
static List<Integer>[] poppedValues;
4+
static Deque<Integer> queue;
5+
static ArrayQueue<Integer> concurrentQueue;
6+
static List<Integer>[] deqValues;
77
static int TH = 10, NUM = 1000;
88

9-
// Each unsafe thread pushes N numbers and pops N, adding
10-
// them to its own poppedValues for checking; using Java's
11-
// sequential stack implementation, ArrayDeque.
9+
// Each unsafe thread enqs N numbers and deqs N, adding
10+
// them to its own deqValues for checking; using Java's
11+
// sequential queue implementation, ArrayDeque.
1212
static Thread unsafe(int id, int x, int N) {
1313
return new Thread(() -> {
14-
String action = "push";
14+
String action = "enq";
1515
try {
1616
for (int i=0, y=x; i<N; i++)
17-
stack.push(y++);
17+
queue.addLast(y++);
1818
Thread.sleep(1000);
19-
action = "pop";
19+
action = "deq";
2020
for (int i=0; i<N; i++)
21-
poppedValues[id].add(stack.pop());
21+
deqValues[id].add(queue.removeFirst());
2222
}
2323
catch (Exception e) { log(id+": failed "+action); }
2424
});
2525
}
2626

27-
// Each safe thread pushes N numbers and pops N, adding
28-
// them to its own poppedValues for checking; using
29-
// ArrayStack.
27+
// Each safe thread enqs N numbers and deqs N, adding
28+
// them to its own deqValues for checking; using
29+
// ArrayQueue.
3030
static Thread safe(int id, int x, int N) {
3131
return new Thread(() -> {
32-
String action = "push";
32+
String action = "enq";
3333
try {
3434
for (int i=0, y=x; i<N; i++)
35-
concurrentStack.push(y++);
35+
concurrentQueue.enq(y++);
3636
Thread.sleep(1000);
37-
action = "pop";
37+
action = "deq";
3838
for (int i=0; i<N; i++)
39-
poppedValues[id].add(concurrentStack.pop());
39+
deqValues[id].add(concurrentQueue.deq());
4040
}
4141
catch (Exception e) { log(id+": failed "+action);
4242
e.printStackTrace(); }
4343
});
4444
}
4545

46-
// Checks if each thread popped N values, and they are
46+
// Checks if each thread dequeued N values, and they are
4747
// globally unique.
4848
static boolean wasLIFO(int N) {
4949
Set<Integer> set = new HashSet<>();
5050
boolean passed = true;
5151
for (int i=0; i<TH; i++) {
52-
int n = poppedValues[i].size();
52+
int n = deqValues[i].size();
5353
if (n != N) {
54-
log(i+": popped "+n+"/"+N+" values");
54+
log(i+": dequeued "+n+"/"+N+" values");
5555
passed = false;
5656
}
57-
for (Integer x : poppedValues[i])
57+
for (Integer x : deqValues[i])
5858
if (set.contains(x)) {
5959
log(i+": has duplicate value "+x);
6060
passed = false;
6161
}
62-
set.addAll(poppedValues[i]);
62+
set.addAll(deqValues[i]);
6363
}
6464
return passed;
6565
}
6666

6767
@SuppressWarnings("unchecked")
6868
static void testThreads(boolean safe) {
69-
stack = new ArrayDeque<>();
70-
concurrentStack = new ArrayQueue<>(TH*NUM);
71-
poppedValues = new List[TH];
69+
queue = new ArrayDeque<>();
70+
concurrentQueue = new ArrayQueue<>(TH*NUM);
71+
deqValues = new List[TH];
7272
for (int i=0; i<TH; i++)
73-
poppedValues[i] = new ArrayList<>();
73+
deqValues[i] = new ArrayList<>();
7474
Thread[] threads = new Thread[TH];
7575
for (int i=0; i<TH; i++) {
7676
threads[i] = safe?
@@ -86,11 +86,11 @@ static void testThreads(boolean safe) {
8686
}
8787

8888
public static void main(String[] args) {
89-
log("Starting "+TH+" threads with sequential stack");
89+
log("Starting "+TH+" threads with sequential queue");
9090
testThreads(false);
9191
log("Was LIFO? "+wasLIFO(NUM));
9292
log("");
93-
log("Starting "+TH+" threads with array stack");
93+
log("Starting "+TH+" threads with array queue");
9494
testThreads(true);
9595
log("Was LIFO? "+wasLIFO(NUM));
9696
log("");

README.md

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
1-
Array stack is a bounded lock-based stack using an
2-
array. It uses a common lock for both push and pop
3-
operations.
1+
Array queue is a bounded lock-based FIFO queue using
2+
an array. It uses 2 separate locks for head and tail.
43

54
```java
6-
push():
7-
1. Lock stack.
8-
2. Try push.
9-
3. Unlock stack.
5+
enq():
6+
1. Lock tail.
7+
2. Try enq.
8+
3. Unlock tail.
109
```
1110

1211
```java
13-
pop():
14-
1. Lock stack.
15-
2. Try pop.
16-
3. Unlock stack.
12+
deq():
13+
1. Lock head.
14+
2. Try deq.
15+
3. Unlock head.
1716
```
1817

1918
```java
20-
tryPush():
21-
1. Ensure stack is not full
22-
2. Save data at top.
23-
3. Increment top.
19+
tryEnq():
20+
1. Ensure queue is not full
21+
2. Save data at tail.
22+
3. Increment tail.
2423
```
2524

2625
```java
27-
tryPop():
28-
1. Ensure stack is not empty.
29-
2. Decrement top.
30-
3. Return data at top.
26+
tryDeq():
27+
1. Ensure queue is not empty.
28+
2. Return data at head.
29+
3. Increment head.
3130
```
3231

3332
```bash
3433
## OUTPUT
35-
Starting 10 threads with sequential stack
36-
7: failed pop
37-
1: failed pop
38-
5: failed pop
39-
8: failed pop
40-
9: failed pop
41-
1: popped 0/1000 values
42-
5: popped 158/1000 values
43-
7: popped 0/1000 values
44-
8: popped 0/1000 values
45-
9: popped 31/1000 values
34+
Starting 10 threads with sequential queue
35+
2: failed enq
36+
5: failed deq
37+
6: failed deq
38+
7: failed deq
39+
8: failed deq
40+
1: failed deq
41+
4: failed deq
42+
9: failed deq
43+
1: dequeued 0/1000 values
44+
2: dequeued 0/1000 values
45+
4: dequeued 0/1000 values
46+
5: dequeued 698/1000 values
47+
6: dequeued 0/1000 values
48+
7: dequeued 0/1000 values
49+
8: dequeued 0/1000 values
50+
9: dequeued 0/1000 values
4651
Was LIFO? false
4752

48-
Starting 10 threads with array stack
53+
Starting 10 threads with array queue
4954
Was LIFO? true
5055
```
5156

52-
See [ArrayStack.java] for code, [Main.java] for test, and [repl.it] for output.
57+
See [ArrayQueue.java] for code, [Main.java] for test, and [repl.it] for output.
5358

54-
[ArrayStack.java]: https://repl.it/@wolfram77/array-stack#ArrayStack.java
55-
[Main.java]: https://repl.it/@wolfram77/array-stack#Main.java
56-
[repl.it]: https://array-stack.wolfram77.repl.run
59+
[ArrayQueue.java]: https://repl.it/@wolfram77/array-queue#ArrayQueue.java
60+
[Main.java]: https://repl.it/@wolfram77/array-queue#Main.java
61+
[repl.it]: https://array-queue.wolfram77.repl.run
5762

5863

5964
### references

0 commit comments

Comments
 (0)