Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BlockingQueue {
- private List queue = new LinkedList();
- private int maxSize = 10;
- public BlockingQueue(int limit){
- this.maxSize = maxSize;
- }
- public synchronized void enqueue(Object item) throws InterruptedException {
- while(this.queue.size() == this.maxSize) {
- wait();
- }
- if(this.queue.size() == 0) {
- notifyAll();
- }
- this.queue.add(item);
- }
- public synchronized Object dequeue() throws InterruptedException{
- while(this.queue.size() == 0){
- wait();
- }
- if(this.queue.size() == this.maxSize){
- notifyAll();
- }
- return this.queue.remove(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement