简体   繁体   中英

Measure calculation time between threads - java

new Thread(new Runnable() {
   @Override public void run() {
    //calculations #1
    }
 }).start();
new Thread(new Runnable() {
   @Override public void run() {
    //calculations #2
    }
}).start();

1) I want to measure the execution time of any thread and then sum into a total-time variable. Can anyone explain me how?

2) If thread has only a cycle inside (for, while) and the cycle ends, also ends the thread?

You basically need a resource that you can access and mutate form multiple threads. An atomic integer could be helpful in your case. See this discussion - Practical uses for AtomicInteger .

To answer your second question - I am assuming your run method has a single while loop and when the loop is exited the control exists even out our run. So yes, when the run exits the thread has completed its work. If you are interested in a more detailed discussion on the lifecycle of a thread check out this post - How to start/stop/restart a thread in Java? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM