Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BlockingQueue<T>
- {
- T[] contents;
- int capacity;
- public BlockingQueue(int capacity)
- {
- contents = (T[]) new Object[capacity];
- this.capacity = capacity;
- }
- public synchronized int getCount()
- {
- for (int i = 0; i < capacity; i++)
- {
- if(contents[i] == null)
- return i;
- }
- return 0;
- }
- public synchronized void enqueue(T item) throws InterruptedException
- {
- int count = this.getCount();
- while (count == this.capacity)
- wait();
- contents[count] = item;
- notifyAll();
- }
- public synchronized T dequeue() throws InterruptedException
- {
- int count = this.getCount();
- while (count == 0)
- wait();
- T item = contents[count];
- contents[count] = null;
- notifyAll();
- return item;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement