-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDequeueMethods.java
More file actions
35 lines (27 loc) · 1.03 KB
/
DequeueMethods.java
File metadata and controls
35 lines (27 loc) · 1.03 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
package com.codecafe.concurrency.blockingqueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class DequeueMethods {
public static void main(String[] args) {
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
// take() blocks until an element becomes available
try {
String element = blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
// poll() returns null if no element is available
String element2 = blockingQueue.poll();
// poll(timeOut, TimeUnit) blocks until given timeOut
// for an element to become available. If no element is available
// before that time then null is returned
try {
String element3 = blockingQueue.poll(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// removes the element if present in the BlockingQueue
boolean wasRemoved = blockingQueue.remove("1");
}
}