title: Optimizing multithreaded queue processing code in Java
body: <p>I have a code that creates 10 objects of a class, that implement runnable.
  Each object is kept in hashmap for later usage.
  Each object is running on a separate thread. Each object has a public method where items can be added into the queue.
  The object processes the queue with infinite loop.</p>
  
  <p>I want to know if this solution is OK or is there something that is totally wrong/useless/missing(especially the use of volatile and syncronized keywords)?</p>
  
  <p><strong>MultithreadingTest.class</strong></p>
  
  <pre><code>package multithreadingtest;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  /**
   * Multithreading example.
   *
   * @author lkallas
   */
  public class MultithreadingTest {
  
      private static final int NUM_OF_THREADS = 10;
      private static String name;
      private static final Map&lt;Integer, ThreadWorker&gt; objectMap = new HashMap&lt;&gt;();    //Map or storing Threadworker objects
  
      public static void main(String[] args) {
          ExecutorService executor = Executors.newFixedThreadPool(NUM_OF_THREADS);
          //Creating threads
          for (int i = 0; i &lt; NUM_OF_THREADS; i++) {
              name = "ThreadWorker" + String.valueOf(i);
              ThreadWorker thread = new ThreadWorker(name);
              objectMap.put(i, thread);   //Add objects to map            
              executor.execute(thread);
          }
          for (int i = 0; i &lt; 10; i++) {
              ThreadWorker worker = objectMap.get(i);
              for (int j = 0; j &lt; 10; j++) {
                  worker.addToQueue("Test1");
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>ThreadWorker.class</strong></p>
  
  <pre><code>package multithreadingtest;
  
  import java.util.LinkedList;
  import java.util.Queue;
  
  /**
   * Worker class that performs operations in another thread.
   *
   * @author lkallas
   */
  public class ThreadWorker implements Runnable {
  
      private final String threadName;
      private volatile Queue workQueue;   //Does this have to volatile??
  
      /**
       * Class constructor.
       *
       * @param threadName Name of the thread for identifying.
       *
       */
      public ThreadWorker(String threadName) {
          this.threadName = threadName;
          this.workQueue = new LinkedList();
          System.out.println(String.format("Thread %s started!", threadName));
      }
  
      /**
       * Adds items to the queue.
       *
       * @param object Object to be added to the queue.
       */
      public synchronized void addToQueue(String object) {
          workQueue.add(object); //Does it have to be syncronized void
      }
  
      @Override
      public void run() {
          while (true) {
              if (!workQueue.isEmpty()) {
                  System.out.println("Queue size: " + workQueue.size());
                  String item = (String) workQueue.peek();
                  //Process item
                  System.out.println(threadName + " just processed " + item);
                  workQueue.remove();
              }
          }
      }
  }
  </code></pre>
  
  <p>Any help and suggestions are much appreciated!</p>