title: Java: Implement own message queue (threadsafe)
body: <p>The task is to implement my own messagequeue that is thread safe. </p>
  
  <p>My approach: </p>
  
  <pre><code>public class MessageQueue {
      /**
       * Number of strings (messages) that can be stored in the queue.
       */
      private int capacity;
  
      /**
       * The queue itself, all incoming messages are stored in here.
       */
      private Vector&lt;String&gt; queue = new Vector&lt;String&gt;(capacity);
  
      /**
       * Constructor, initializes the queue.
       * 
       * @param capacity The number of messages allowed in the queue.
       */
      public MessageQueue(int capacity) {
          this.capacity = capacity;
      }
  
      /**
       * Adds a new message to the queue. If the queue is full,
       * it waits until a message is released. 
       *
       * @param message
       */
      public synchronized void send(String message) {
          //TODO check
      }
  
      /**
       * Receives a new message and removes it from the queue. 
       *
       * @return
       */
      public synchronized String receive() {
          //TODO check
          return "0";
      }
  }
  </code></pre>
  
  <p>If the queue is empty and I call remove(), I want to call wait() so that another thread can use the send() method. Respectively, I have to call notifyAll() after every iteration. </p>
  
  <p>Question: Is that possible? I mean does it work that when I say wait() in one method of an object, that I can then execute another method of the same object? </p>
  
  <p>And another question: Does that seem to be clever? </p>