Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In general you cannot measure time execution exactly because context switching. If you have 1000 threads it is produce significant impact to all time measuring. Nevertheless if you may omit high exactness of time measuring you may use CountDownLautching primitive to synchronizing thread starting and thread finishing:</p> <pre><code> public static void main( String[] args ) throws InterruptedException { final int THREAD_COUNT = 10000; long startTime; long endTime; final CountDownLatch startBarierr = new CountDownLatch(THREAD_COUNT + 1); final CountDownLatch finishBarierr = new CountDownLatch(THREAD_COUNT); for (int i = 0; i &lt; THREAD_COUNT; i++){ final int iterationIndex = i; new Thread(new Runnable() { @Override public void run() { startBarierr.countDown(); System.out.println("Thread " + iterationIndex + " started"); try { startBarierr.await(); //do some work in separate thread int iterationCount = (int)(0.8*Integer.MAX_VALUE); for(int i = 0; i &lt; iterationCount; i++){ } System.out.println("Thread " + iterationIndex + " finished"); finishBarierr.countDown(); //current thread finished, send mark } catch (InterruptedException e) { throw new AssertionError("Unexpected thread interrupting"); } } }).start(); } startBarierr.countDown(); startBarierr.await(); //await start for all thread startTime = System.currentTimeMillis(); //and note time finishBarierr.await(); //wait each thread endTime = System.currentTimeMillis(); //note finish time System.out.println("Time(ms) - " + (endTime - startTime)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload