java BlockinOueue
java BlockinOueue
Methods of BlockingQueue
Based on whether a queue is full or empty, methods of a blocking
queue can be divided into 3 categories:
Here,
This means the offer() method will try to insert an element to the
blocking queue for 100 milliseconds. If the element cannot be inserted
in 100 milliseconds, the method returns false.
Note: Instead of milliseconds, we can also use these time
units: days, hours, minutes, seconds, microseconds and nanoseconds i
n offer() and poll() methods.
class Main {
try {
// Insert element to blocking queue
numbers.put(2);
numbers.put(1);
numbers.put(3);
System.out.println("BLockingQueue: " + numbers);
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
BlockingQueue: [2, 1, 3]
Removed Element: 2