Skip to main content
added 115 characters in body
Source Link
tomasb
  • 1.7k
  • 2
  • 23
  • 30

Or don't interrupt, its nasty.

 public class MyQueue<T> extends ArrayBlockingQueue<T> {     private static final long serialVersionUID = 1L;   private boolean done = false;     public ParserQueue(int capacity) { super(capacity); }     public void done() { done = true; }     public boolean isDone() { return done; }     /**   * May return null if producer ends the production after consumer  * has entered the element-await loopstate.   */   public T take() throws InterruptedException {   T el;   while ((el = super.poll()) == null && !done) {   synchronized (this) {   wait();   }   }     return el;   }  } 
  1. when producer puts object to the queue, call queue.notify(), if it ends, call queue.done()
  2. loop while (!queue.isDone() || !queue.isEmpty())
  3. test take() return value for null

Or don't interrupt, its nasty.

public class MyQueue<T> extends ArrayBlockingQueue<T> { private static final long serialVersionUID = 1L; private boolean done = false; public ParserQueue(int capacity) { super(capacity); } public void done() { done = true; } public boolean isDone() { return done; } /** * May return null if producer ends the production after consumer has entered the element-await loop. */ public T take() throws InterruptedException { T el; while ((el = super.poll()) == null && !done) { synchronized (this) { wait(); } } return el; } } 
  1. when producer puts object to the queue, call queue.notify(), if it ends, call queue.done()
  2. loop while (!queue.isDone() || !queue.isEmpty())
  3. test take() return value for null

Or don't interrupt, its nasty.

 public class MyQueue<T> extends ArrayBlockingQueue<T> {     private static final long serialVersionUID = 1L;   private boolean done = false;     public ParserQueue(int capacity) { super(capacity); }     public void done() { done = true; }     public boolean isDone() { return done; }     /**   * May return null if producer ends the production after consumer  * has entered the element-await state.   */   public T take() throws InterruptedException {   T el;   while ((el = super.poll()) == null && !done) {   synchronized (this) {   wait();   }   }     return el;   }  } 
  1. when producer puts object to the queue, call queue.notify(), if it ends, call queue.done()
  2. loop while (!queue.isDone() || !queue.isEmpty())
  3. test take() return value for null
Source Link
tomasb
  • 1.7k
  • 2
  • 23
  • 30

Or don't interrupt, its nasty.

public class MyQueue<T> extends ArrayBlockingQueue<T> { private static final long serialVersionUID = 1L; private boolean done = false; public ParserQueue(int capacity) { super(capacity); } public void done() { done = true; } public boolean isDone() { return done; } /** * May return null if producer ends the production after consumer has entered the element-await loop. */ public T take() throws InterruptedException { T el; while ((el = super.poll()) == null && !done) { synchronized (this) { wait(); } } return el; } } 
  1. when producer puts object to the queue, call queue.notify(), if it ends, call queue.done()
  2. loop while (!queue.isDone() || !queue.isEmpty())
  3. test take() return value for null