Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class BlockingQueue<T>
  2. {
  3. T[] contents;
  4. int capacity;
  5.  
  6. public BlockingQueue(int capacity)
  7. {
  8. contents = (T[]) new Object[capacity];
  9. this.capacity = capacity;
  10. }
  11.  
  12. public synchronized int getCount()
  13. {
  14. for (int i = 0; i < capacity; i++)
  15. {
  16. if(contents[i] == null)
  17. return i;
  18. }
  19.  
  20. return 0;
  21. }
  22.  
  23. public synchronized void enqueue(T item) throws InterruptedException
  24. {
  25. int count = this.getCount();
  26. while (count == this.capacity)
  27. wait();
  28.  
  29. contents[count] = item;
  30. notifyAll();
  31. }
  32.  
  33. public synchronized T dequeue() throws InterruptedException
  34. {
  35. int count = this.getCount();
  36. while (count == 0)
  37. wait();
  38.  
  39. T item = contents[count];
  40.  
  41. contents[count] = null;
  42.  
  43. notifyAll();
  44.  
  45. return item;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement