简体   繁体   English

测量线程之间的计算时间-Java

[英]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. 1)我想测量任何线程的执行时间,然后求和成一个总时间变量。 Can anyone explain me how? 谁能解释一下我呢?

2) If thread has only a cycle inside (for, while) and the cycle ends, also ends the thread? 2)如果线程内部只有一个循环(for,while)并且该循环结束了,线程也结束了吗?

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 . 请参阅此讨论-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. 要回答您的第二个问题-我假设您的run方法具有一个while循环,并且当退出循环时,该控件甚至在我们的运行中都存在。 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? 如果您对线程生命周期的更详细讨论感兴趣,请查看这篇文章- 如何在Java中启动/停止/重新启动线程? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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