Skip to content

Commit 190f5dc

Browse files
committed
[Function add]
1.Add test for array blocking queue.
1 parent 04188fa commit 190f5dc

File tree

5 files changed

+134
-14
lines changed

5 files changed

+134
-14
lines changed

Algorithm(4th_Edition)/src/ca/mcmaster/queue/arrayblockingqueue/ArrayBlockingQueueConsumer.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ public ArrayBlockingQueueConsumer(ArrayBlockingQueue<Integer> queue) {
1111
@Override
1212
public void run() {
1313
try {
14-
while(true){
15-
System.out.println("consumer...");
16-
// Thread.currentThread().join(10);
17-
// System.out.println("Consumer: get " + queue.take() + " from queue...");
14+
for(int i = 0; i < 100; i++){
15+
Thread.currentThread().join(10);
16+
System.out.println("Consumer: get " + queue.take() + " from queue...");
1817
Thread.sleep(10);
1918
}
2019
} catch (InterruptedException e) {
@@ -23,10 +22,10 @@ public void run() {
2322
}
2423

2524
public static void main(String[] args) throws InterruptedException {
26-
// ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10000);
27-
new Thread().run();
28-
new Thread(()->{System.out.println("producer...");}).run();
29-
// Thread.currentThread().join();
25+
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
26+
new Thread(new ArrayBlockingQueueConsumer(queue)).start();
27+
new Thread(new ArrayBlockingQueueProducer(queue)).start();
28+
Thread.currentThread().join();
3029
System.out.println("Finish...");
3130
}
3231
}

Algorithm(4th_Edition)/src/ca/mcmaster/queue/arrayblockingqueue/ArrayBlockingQueueProducer.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ public ArrayBlockingQueueProducer(ArrayBlockingQueue<Integer> queue) {
1414
public void run() {
1515
while(true){
1616
try {
17-
System.out.println("producer...");
18-
Thread.currentThread().join(10);
19-
// queue.put(ai.getAndIncrement());
20-
// System.out.println("Producer: put " + ai.get() + " into queue...");
21-
Thread.sleep(10000);
17+
queue.put(ai.getAndIncrement());
18+
System.out.println("Producer: put " + ai.get() + " into queue...");
19+
Thread.sleep(10);
2220
} catch (InterruptedException e) {
2321
e.printStackTrace();
2422
}

DataStructrue/Queue/ArrayBlockingQueue.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,71 @@
133133
lock.unlock();
134134
}
135135
}
136-
```
136+
```
137+
138+
## Test
139+
* Producer
140+
Producer continuously put data into queue.
141+
```Java
142+
public class ArrayBlockingQueueProducer implements Runnable {
143+
private ArrayBlockingQueue<Integer> queue;
144+
public ArrayBlockingQueueProducer(ArrayBlockingQueue<Integer> queue) {
145+
super();
146+
this.queue = queue;
147+
}
148+
private volatile AtomicInteger ai = new AtomicInteger(0);
149+
@Override
150+
public void run() {
151+
while(true){
152+
try {
153+
queue.put(ai.getAndIncrement());
154+
System.out.println("Producer: put " + ai.get() + " into queue...");
155+
Thread.sleep(10);
156+
} catch (InterruptedException e) {
157+
e.printStackTrace();
158+
}
159+
}
160+
}
161+
}
162+
```
163+
164+
* Consumer
165+
Consumer get take data from queue 100 times
166+
```Java
167+
public class ArrayBlockingQueueConsumer implements Runnable {
168+
private ArrayBlockingQueue<Integer> queue;
169+
public ArrayBlockingQueueConsumer(ArrayBlockingQueue<Integer> queue) {
170+
super();
171+
this.queue = queue;
172+
}
173+
@Override
174+
public void run() {
175+
try {
176+
for(int i = 0; i < 100; i++){
177+
Thread.currentThread().join(10);
178+
System.out.println("Consumer: get " + queue.take() + " from queue...");
179+
Thread.sleep(10);
180+
}
181+
} catch (InterruptedException e) {
182+
e.printStackTrace();
183+
}
184+
}
185+
186+
public static void main(String[] args) throws InterruptedException {
187+
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
188+
new Thread(new ArrayBlockingQueueConsumer(queue)).start();
189+
new Thread(new ArrayBlockingQueueProducer(queue)).start();
190+
Thread.currentThread().join();
191+
System.out.println("Finish...");
192+
}
193+
}
194+
```
195+
* Result
196+
>...
197+
Producer: put 108 into queue...
198+
Consumer: get 98 from queue...
199+
Producer: put 109 into queue...
200+
Consumer: get 99 from queue...
201+
Producer: put 110 into queue...
202+
203+
We can find that Producer is **blocked**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ca.mcmaster.queue.arrayblockingqueue;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
5+
public class ArrayBlockingQueueConsumer implements Runnable {
6+
private ArrayBlockingQueue<Integer> queue;
7+
public ArrayBlockingQueueConsumer(ArrayBlockingQueue<Integer> queue) {
8+
super();
9+
this.queue = queue;
10+
}
11+
@Override
12+
public void run() {
13+
try {
14+
for(int i = 0; i < 100; i++){
15+
Thread.currentThread().join(10);
16+
System.out.println("Consumer: get " + queue.take() + " from queue...");
17+
Thread.sleep(10);
18+
}
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
24+
public static void main(String[] args) throws InterruptedException {
25+
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
26+
new Thread(new ArrayBlockingQueueConsumer(queue)).start();
27+
new Thread(new ArrayBlockingQueueProducer(queue)).start();
28+
Thread.currentThread().join();
29+
System.out.println("Finish...");
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ca.mcmaster.queue.arrayblockingqueue;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
public class ArrayBlockingQueueProducer implements Runnable {
7+
private ArrayBlockingQueue<Integer> queue;
8+
public ArrayBlockingQueueProducer(ArrayBlockingQueue<Integer> queue) {
9+
super();
10+
this.queue = queue;
11+
}
12+
private volatile AtomicInteger ai = new AtomicInteger(0);
13+
@Override
14+
public void run() {
15+
while(true){
16+
try {
17+
queue.put(ai.getAndIncrement());
18+
System.out.println("Producer: put " + ai.get() + " into queue...");
19+
Thread.sleep(10);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)