优化 Java 代码以实现快速响应

我有一个多线程 Java 应用程序,它使用多个 CPU 密集型线程来收集信息。每隔几分钟,就会发现需要由程序的另一个线程处理的结果。将找到的结果添加到列表中,并通知其他相关线程(使用 Lock 和 Condition),然后处理找到的信息。我需要将这些信息从一个线程传递到另一个线程的时间延迟尽可能小。使用 System.currentTimeMillis() 测量唤醒和通知之间的时间时,延迟通常小于 5 毫秒,最常见的是小于或等于 1 毫秒。有时,延迟更大(10-20ms)。由于毫秒是计算机的宏单位,我认为可靠地小于 1ms 的延迟应该是可能的,这将有利于我的应用程序。

你知道更大延迟的原因是什么,或者我如何找到去哪里找?会不会是垃圾回收?或者线程唤醒的几毫秒变化是否被认为是正常的?

我在 Linux Ubuntu 虚拟专用服务器上使用 Java 版本 1.8.0。

附上程序设计的一个例子。运行它不会正确模拟我的生产程序观察到的延迟。“实际”程序使用大量内存、CPU,并且每隔几分钟只传输一点信息。我尝试过但未能简单地模拟这一点。


30秒到达战场
浏览 176回答 2
2回答

暮色呼如

System.currentTimeMillis() 可能会受到时钟漂移的影响,通常粒度约为 10 毫秒。要测量经过的时间,您应该始终使用System.nanoTime()它,因为它可以保证准确性。

守着星空守着你

它可能不会加快您的进程,但使用 aBlockingQueue肯定会使代码更清晰。还请注意Thread.sleep没有信息时的for。final BlockingQueue<Long> queue = new ArrayBlockingQueue<>(10);private void startWatcherThread() {&nbsp; &nbsp; Thread t = new Thread() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Waiting for results...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long polled = queue.poll(1, TimeUnit.SECONDS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do something with info&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; t.start();}private void startInfoThreads() {&nbsp; &nbsp; for (int i = 0; i < 14; i++) {&nbsp; &nbsp; &nbsp; &nbsp; Thread t = new Thread() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random r = new Random();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Gather info, 'hits' about once every few minutes!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean infoRandomlyFound = r.nextInt(100) >= 99;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (infoRandomlyFound) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queue.put(System.currentTimeMillis());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; t.start();&nbsp; &nbsp; }}private void test() {&nbsp; &nbsp; startInfoThreads();&nbsp; &nbsp; startWatcherThread();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java