title: Does Java actually run threads in parallel
body: <p>I would like to understand that does java actually run multiple threads in parallel in a multi core CPU, or there is context switching between threads and only one thread is active and others are waiting for their turn to run.</p>
  
  <p>In other words, is there a possibility that 2 threads are running in parallel??? </p>
  
  <p>Because my Thread.currentThread() does not give me a array of threads, but only one thread which is running.
  So what is the truth, does only one thread run at a time while others wait or multiple threads can run in parallel, if yes , then why my Thread.currentThread() method return only 1 thread object.</p>
  
  <p>Edit : .....</p>
  
  <p>I have created 2 classes to count numbers
  1 class does it synchronously and the other one divides it into two halves and executes the two halves in 2 threads..(intel i5(4 CPUs), 8GB ram)
  the code is as follows :</p>
  
  <p>common class :</p>
  
  <pre><code>class  Answer{
  long ans = 0L;}
  </code></pre>
  
  <p>Multi Thread execution :
  public class Sheet2 {</p>
  
  <pre><code>public static void main(String[] args) {
  
      final Answer ans1 = new Answer();
      final Answer ans2 = new Answer();
  
  
      Thread t1 = new Thread(new Runnable() {
  
          @Override
          public void run() {
              for(int i=0;i&lt;=500000; i++) {
                  ans1.ans = ans1.ans + i;
              }
          }
      });
  
      Thread t2 = new Thread(new Runnable() {
  
          @Override
          public void run() {
              for(int i=500001;i&lt;=1000000; i++) {
                  ans2.ans = ans2.ans + i;
              }
          }
      });
      long l1 = System.currentTimeMillis();
  
      try {
          t1.start();t2.start();
          t1.join();
          t2.join();
          long l2 = System.currentTimeMillis();
          System.out.println("ans :" + (ans1.ans + ans2.ans) +" in "+(l2-l1) +" milliseconds"); 
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Single Thread execution :
  public class Sheet3 {</p>
  
  <pre><code>public static void main(String[] args) {
  
      final Answer ans1 = new Answer();
  
      long l1 = System.currentTimeMillis();
  
      for(int i=0;i&lt;=1000000; i++) {
          ans1.ans = ans1.ans + i;
      }
      long l2 = System.currentTimeMillis();
  
      System.out.println("ans :" + (ans1.ans ) +" in "+(l2-l1) +" milliseconds"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My Single thread execution is faster than my multi threaded execution, I though the context switching was putting a overhead on the execution initially and hence the multithreaded execution output was slower, now I am having muti core CPU (4 CPU), but still single threaded execution is faster in this example..</p>
  
  <p>Can you please explain the scenario here... is it because my other processes are eating up the other cores and hence my threads are not running in parallel and performing time slicing on the CPU ???</p>
  
  <p>Kindly throw some light on this topic.
  Thanks in advance.
  Cheers.!!! </p>