title: Implementing a synchronized queue in Java using ArrayList
body: <p>I've been working on a project where I need a synchronized queue, for the reason that my program is multi-threaded and the thread may access this queue.
  I used arraylist to do that, but I seem to have some issues with it and threads got deadlocked. I don't know if the queue is the reason, but I just wanted to check:</p>
  
  <pre><code>public class URLQueue {
      private ArrayList&lt;URL&gt; urls;
  
      public URLQueue() {
          urls = new ArrayList&lt;URL&gt;();
      }
  
      public synchronized URL remove() throws InterruptedException {
          while (urls.isEmpty())
              wait();
          URL r = urls.remove(0);
          notifyAll();
          return r;
      }
  
      public synchronized void add(URL newURL) throws InterruptedException {
          urls.add(newURL);
          notifyAll();
      }
  
      public int getSize() {
          return urls.size();
      }
  }
  </code></pre>
  
  <p>EDITS:
  Even when using LinkedBlockingQueue I get stuck in the same loop as before. I think this is caused because there is a thread which is waiting for the queue to be filled, but it never does because the other functionalities are done running...any ideas???</p>