简体   繁体   English

Java中的并发性如何提高在单核处理器上运行的程序的性能?

[英]How concurrency in java improves the performance of program running on single core processor?

Recently I have read in a book that Concurrency is a fundamental tool for multiprocessor programming. 最近,我在一本书中读到,并发是用于多处理器编程的基本工具。

Than how is it useful for single core processor? 比起单核处理器有什么用?

Concurrency is a fundamental tool for multiprocessor programming. 并发是用于多处理器编程的基本工具。

Yes, but it can also help in other areas. 是的,但是它在其他方面也可以提供帮助。 For instance, concurrency can also improve throughput on a single core system if the cpu is not the bottleneck (for instance, because the threads spend most of their time waiting for I/O from disk or the network). 例如,如果cpu并非瓶颈,那么并发还可以提高单个核心系统的吞吐量(例如,因为线程花费了大部分时间来等待来自磁盘或网络的I / O)。

Concurrency is helpful whenever there are multiple tasks that need to be run simultaneously. 当有多个任务需要同时运行时,并发很有用。 A very common example is in GUI programming; 一个非常常见的示例是GUI编程。 you don't want your UI to freeze up while the program waits for data to load from the disk or network, so you have a thread that just manages the UI elements (called the Event Dispatch Thread in Swing/AWT), and background threads that take care of communications or long-running calculations. 您不希望您的UI在程序等待从磁盘或网络加载数据时冻结,因此您有一个仅管理UI元素的线程(在Swing / AWT中称为“事件调度线程”)和后台线程负责沟通或长期运行的计算。 The OS swaps them back and forth to make sure that the UI gets redrawn while other tasks are still active. 操作系统来回交换它们,以确保在其他任务仍处于活动状态时重绘UI。

Being able to switch between running thread is useful for the perception of performance on a single core processor. 能够在运行线程之间切换对于感知单个核心处理器的性能很有用。 In many systems, the graphical parts of the UI is updated by another thread than the one doing the work. 在许多系统中,UI的图形部分是由另一个线程而不是由工作线程来更新的。 It would be frustrating for the user to not have any feedback while a long task is running. 用户在长时间运行任务时没有任何反馈将令人沮丧。 Another useful application of concurrency on a single core, would be to start a long running task with low priority while the user is free to do other stuff at the same time. 并发在单核上的另一个有用的应用程序是以低优先级启动长时间运行的任务,而用户可以自由地同时做其他事情。

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

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