Java性能进程与线程 [英] Java Performance Processes vs Threads

查看:124
本文介绍了Java性能进程与线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java实现一个工作池。

I am implementing a worker pool in Java.

这实际上是一整堆对象,它们将获取数据块,处理数据然后存储结果。由于IO延迟,工作人员将远远多于处理器核心。

This is essentially a whole load of objects which will pick up chunks of data, process the data and then store the result. Because of IO latency there will be significantly more workers than processor cores.

服务器专用于此任务,我想从硬件中获取最大性能(但是不,我不想在C ++中实现它。

The server is dedicated to this task and I want to wring the maximum performance out of the hardware (but no I don't want to implement it in C++).

最简单的实现是使用单个Java进程来创建和监视许多工作线程。另一种方法是为每个worker运行一个Java进程。

The simplest implementation would be to have a single Java process which creates and monitors a number of worker threads. An alternative would be to run a Java process for each worker.

为了论证,假设有四核Linux服务器,你会预期这些解决方案中的哪一种更具性能?为什么?

Assuming for arguments sake a quadcore Linux server which of these solutions would you anticipate being more performant and why?

你可以假设工人永远不需要彼此沟通。

You can assume the workers never need to communicate with one another.

推荐答案

一个进程,多个线程 - 原因有几个。

One process, multiple threads - for a few reasons.

当在作业之间进行上下文切换时,某些处理器之间在线程之间切换比在进程之间切换更便宜。在这种I / O限制案例中,这一点尤为重要,因为工作人员多于核心。在阻止I / O之间做的工作越多,这就越不重要。但是,良好的缓冲将为线程进程付出代价。

When context-switching between jobs, it's cheaper on some processors to switch between threads than between processes. This is especially important in this kind of I/O-bound case with more workers than cores. The more work you do between getting I/O blocked, the less important this is. Good buffering will pay for threads or processes, though.

在同一JVM中的线程之间切换时,至少某些Linux实现(特别是x86)不需要刷新缓存。 查看Tsuna的博客 。线程之间的缓存污染将最小化,因为它们可以共享程序缓存,执行相同的任务,并共享相同的代码副本。我们说的是每个开关大约100纳秒到几微秒的节省。如果这对你来说是小土豆,请继续阅读...

When switching between threads in the same JVM, at least some Linux implementations (x86, in particular) don't need to flush cache. See Tsuna's blog. Cache pollution between threads will be minimized, since they can share the program cache, are performing the same task, and are sharing the same copy of the code. We're talking savings on the order of 100's of nanoseconds to several microseconds per switch. If that's small potatoes for you, then read on...

根据设计,一个流程的I / O数据路径可能会更短。

Depending on the design, the I/O data path may be shorter for one process.

线程的启动和预热时间通常要短得多。操作系统不必启动进程,Java不必启动另一个JVM,类加载只执行一次,JIT编译只执行一次,HotSpot优化只执行一次,并且更快。

The startup and warmup time for a thread is generally much shorter. The OS doesn't have to start a process, Java doesn't have to start another JVM, classloading is only done once, JIT-compilation is only done once, and HotSpot optimizations are done once, and sooner.

这篇关于Java性能进程与线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆