title: Java: Sending messages to a JMS queue with multiple threads
body: <p>I am trying to write a Java class to both send and read messages from a JMS queue using multiple threads to speed things up. I have the below code.</p>
  
  <pre><code>    System.out.println("Sending messages");
      long startTime = System.nanoTime();
  
      Thread threads[] = new Thread[NumberOfThreads];
      for (int i = 0; i &lt; threads.length; i ++) {
          threads[i] = new Thread() {
              public void run() {
                  try {
                      for (int i = 0; i &lt; NumberOfMessagesPerThread; i ++) {
                          sendMessage("Hello");
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          };
          threads[i].start();
      }
  
      //Block until all threads are done so we can get total time
      for (Thread thread : threads) {
          thread.join();
      }
  
      long endTime = System.nanoTime();
      long duration = (endTime - startTime) / 1000000;
      System.out.println("Done in " + duration + " ms");
  </code></pre>
  
  <p>This code works and sends however many messages to my JMS queue that I say (via NumberOfThreads and NumberOfMessagesPerThread). However, I am not convinced it is truly working multithreaded. For example, if I set threads to 10 and messages to 100 (so 1000 total messages), it takes the same time as 100 threads and 10 messages each. Even this code below takes the same time.</p>
  
  <pre><code>    for (int i = 0; i &lt; 1000; i ++) {
          sendMessage("Hello");
      }
  </code></pre>
  
  <p>Am I doing the threading right? I would expect the multithreaded code to be much faster than just a plain for loop.</p>