title: Java: how to test fine-granularity in multithreaded program
body: <p>In Java, I have simple multithreaded code:</p>
  
  <pre><code>public class ThreadedAlgo {
  
      public static final int threadsCount = 3;
  
      public static void main(String[] args) {
  
          // start timer prior computation
          time = System.currentTimeMillis();
  
          // create threads
          Thread[] threads = new Thread[threadsCount];
  
          class ToDo implements Runnable {
              public void run() { ... }
          }
  
          // create job objects
          for (int i = 0; i &lt; threadsCount; i++) {
              ToDo job = new ToDo();
              threads[i] = new Thread(job);
          }
  
          // start threads
          for (int i = 0; i &lt; threadsCount; i++) {
              threads[i].start();
          }
  
          // wait for threads above to finish
          for (int i = 0; i &lt; threadsCount; i++) {
              try {
                  threads[i].join();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
          // display time after computation
          System.out.println("Execution time: " + (System.currentTimeMillis() - time));
  
      }
  
  }
  </code></pre>
  
  <p>It works fine, now I want to run it for 2 or 3 threads and compute the time spent for computation of each thread. Then I will compare times: note them by <code>t1</code> and <code>t2</code>, and if <code>|t1 - t2| &lt; small epsilon</code>, I will say that my algorithm performs with fine granularity under some given conditions, that is the time spent by threads is relatively the same.</p>
  
  <p>How can I measure the time of a thread?</p>