- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个线程从 ArrayBlockingQueue() connectionPool.
获取一个对象如果 ArrayBlockingQueue() 为空,则线程可能会被阻塞。为了测量调用线程被阻塞的时间,我使用以下代码:
long start = System.nanoTime();
DataBaseEndPoint dbep = connectionPool.take();
long end = System.nanoTime();
long elapsed = (end - start)/1000000;
现在,我担心的是,未阻塞的线程可能会开始在多处理器计算机中的不同处理器上运行。在这种情况下,我不完全确定使用的“系统计时器”在不同处理器上是否相同。这篇博客文章 ( http://www.javacodegeeks.com/2012/02/what-is-behind-systemnanotime.html ) 建议 Linux 对每个处理器使用不同的时间戳计数器(也用于 System.nanotime()),这确实会扰乱上面示例中的耗时计算。
The value is read from clock_gettime with CLOCK_MONOTONIC flag Which uses either TSC or HPET. The only difference with Windows is that Linux not even trying to sync values of TSC read from different CPUs, it just returns it as it is. It means that value can leap back and jump forward with dependency of CPU where it is read.
但是,此链接 ( http://lwn.net/Articles/209101/ ) 表明 TSC 不再用于高分辨率计时器。
... the recently-updated high-resolution timers and dynamic tick patch set includes a change which disables use of the TSC. It seems that the high-resolution timers and dynamic tick features are incompatible with the TSC...
所以,问题是,Linux 机器使用什么来将值返回到 System.nanotime()
现在?并且,正在使用System.nanotime()
在上述情况下可以安全地测量耗时(在另一个处理器上启动的阻塞线程)。如果不安全,还有什么替代方案?
最佳答案
虚拟机(以及一般生活)的一个无价之宝就是抽象。线程的执行时间不会根据核心数量而有所不同;不在 Linux 中,也不在 Windows 等中......我希望我没有误解你的问题。
(虽然我使用的是currentTimeMillis(),当然,不同尺度的纳米时间是相同的)
检查我制作的以下示例:
public class SynchThreads {
public static void main(String[] args) throws InterruptedException {
GreedyTask gtA = new GreedyTask("A");
GreedyTask gtB = new GreedyTask("B");
Thread a = new Thread(gtA);
Thread b = new Thread(gtB);
a.start();
b.start();
a.join();
b.join();
System.out.println(gtA.toString()+" running time: "+gtA.getRunningTime());
System.out.println(gtB.toString()+" running time: "+gtB.getRunningTime());
}
private static class GreedyTask implements Runnable {
private long startedTime, finishedTime, totalRunTime;
private String myName;
public GreedyTask(String pstrName) {
myName = pstrName;
}
public void run() {
try {
startedTime = System.currentTimeMillis();
randomPowerNap(this);
finishedTime = System.currentTimeMillis();
totalRunTime = finishedTime - startedTime;
} catch (Exception e) { System.err.println(e.getMessage()); }
}
public String toString() { return ("Task: " + myName); }
public long getRunningTime() { return this.totalRunTime; }
}
private static synchronized void randomPowerNap(GreedyTask gt) throws InterruptedException {
System.out.println("Executing: "+gt.toString());
long random = Math.round(Math.random()*15000);
System.out.println("Random time for "+gt+" is: "+random);
Thread.sleep(random);
}
}
以下是在 4 核 Windows 机器上运行的输出:
Executing: Task: A
Random time for Task: A is: 1225
Executing: Task: B
Random time for Task: B is: 4383
Task: A running time: 1226
Task: B running time: 5609 // what's funny about this? this is equal to Btime - Atime
这是在 4 核 Linux 机器上运行的:
Executing: Task: A
Random time for Task: A is: 13577
Executing: Task: B
Random time for Task: B is: 5340
Task: A running time: 13579
Task: B running time: 18920 // same results
结论:B 总时间增加了 randomPowerNap 被 A 阻塞时必须等待的时间,因此,由于虚拟机的硬件抽象,线程的运行时间没有差异,因为它们都在“虚拟大核心”中运行,如果你明白我的意思的话。
我希望这有帮助。
关于Linux 上的 Java : Measuring elapsed time using System. nanotime() 用于阻塞线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557511/
大家好,我有一段代码如下所示: public class Test { public static void main(String args[]) { long a = Sy
我做了这样的基准: for (int i = 0; i < 1000 * 1000; ++i) { long var = System.nanoTime(); } 在我的jdk6.0电脑上需要
我正在做一个项目,需要在 999 个随机 double 组中实现冒泡排序、希尔排序和快速排序算法。 我需要测量每个算法运行所需的时间。 我正在使用 System.nanoTime() 来测量每个算法的
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个线程将数据插入队列,另一个线程从队列中读取数据并进行处理。我想检查数据在处理之前在队列中的停留时间。 我在第一个线程推送之前在数据中添加了一个时间参数(使用 System.nanoTime()
可以使用 Clock 模拟调用,例如 System.currentTimeMillis() 使用 Clock.millis() 并注入(inject) Clock 的模拟实现。 是否有类似的方法可以轻
我想以纳秒为单位计算两个事件之间耗时。为此,我可以使用 System.nanoTime()如前所述 here .问题是这两个事件发生在不同的线程中。 自 nanoTime()不返回绝对时间戳,而只能用
我想做与下面这段代码相同的操作,但使用 System.nanoTime()反而 new Timer().schedule(new TimerTask() {
我正在编写一个 FTP 客户端应用程序,并在代码中的两个点使用了 System.nanoTime(),返回的秒数差异为 18,而我的程序只需要 2 秒...检查 onPostExecute 方法中的日
我目前正在使用 Java 编写多线程程序。在某些时候,不同的线程会记录它们的操作,我正在使用 nanoTime为了这。每个线程都有自己的日志文件,最后我将它们合并并根据时间 (nanoTime) 对它
我在我的应用程序中使用 system.nanoTime 并计算相对于开始时间的时间。 我的应用程序运行得很好,直到我将其复制到一台新计算机上,在它上纳米时间给了我较慢的值。 我编写了一个简单的应用程序
这不是我第一次遇到编程语言中的时钟时间问题。基本上,我通过在 while 循环中调用函数来测量函数的运行速度。问题是,由于某种原因,while 循环运行的时间越长,耗时就越短。谁能解释一下吗?代码如下
我使用以下代码来评估我的方法/操作的性能- 问题:由于这是贯穿代码的,是否应将其从生产代码中删除以防止降低应用性能。 在我们的一次讨论中,有人指出 Linux(时区)上的一些时间调用会执行 IO 以查
在 gemfire 上,我有一个区域 ABC,并且在该区域内有一个 long 类型的列/字段。在此字段中,我将值设置为 System.nanoTime()。我想获取该区域 24 小时前的所有记录。我怎
我正在编写一个使用 System.nanotime 的 android 计时器应用程序。问题是它给了我低估的结果和负数。 redVal、blueVal 和 greenVal 在相机的每一帧上更新。 结
我正在开发一个 2d java 游戏,但我遇到了计时器问题。这是我的游戏循环: void gameLoop(isRunning){ .... doStuff(); .... } 我在循环中有一个像这样
我正在关注这个jmh 测试http://hg.openjdk.java.net/code-tools/jmh/file/57623b7f64e6/jmh-core-benchmarks/src/mai
我有一个很长的字符串,其模式为 最后,我试图测试一些函数调用的性能,所以我进行了以下测试以试图找出答案......但我认为我可能错误地使用了 nanoTime?因为无论我如何交换顺序,结果都没有意义.
我使用变量 time_of_last_call 作为我的时间起点;因为 nanoTime() 可能会给出负值,所以我不能使用 0 作为我的时间起点来初始化 time_of_last_call。如果我用
我有一个多线程 Java 程序,可以在几秒钟内创建数百个临时文件。这些文件位于 /tmp 中,并使用 System.nanoTime() 命名。 是否保证文件名是唯一的? 最佳答案 不,不能保证每次调
我是一名优秀的程序员,十分优秀!