Search code examples
javamultithreadingmeasure

How can I measure thread waiting time?


I couldn't find out how to measure the time that a Thread is waiting locked. I have to determine if a Thread is waiting locked more than 1 second and if so to run another Thread instead. Thanks!


Solution

  • Try this:

    long startTime = System.nanoTime();
    methodToTime();
    long endTime = System.nanoTime();
    
    long duration = endTime - startTime;