且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java - 多线程代码在更多内核上运行速度更快

更新时间:2023-02-06 21:30:20

添加更多线程并不一定能保证提高性能。使用其他线程可能会导致性能下降的原因有很多:

Adding more threads is not necessarily guarenteed to improve performance. There are a number of possible causes for decreased performance with additional threads:


  • 粗粒度锁定可能过度序列化执行 - 即锁定可能导致一次只运行一个线程。您可以获得多个线程的所有开销,但没有任何好处。尝试减少锁定的持续时间。

  • 这同样适用于过于频繁的障碍和其他同步结构。如果内部 j 循环快速完成,您可能会将大部分时间花在障碍上。尝试在同步点之间做更多的工作。

  • 如果代码运行得太快,可能没有时间将线程迁移到其他CPU核心。除非你创建了许多非常短暂的线程,否则这通常不是问题。使用线程池,或者只是让每个线程更多工作可以提供帮助。如果你的线程运行的时间超过一秒左右,这不太可能是一个问题。

  • 如果你的线程正在处理很多共享的读/写数据,那么缓存行反弹可能会降低性能。也就是说,虽然这通常会导致性能下降,但仅凭这一点不太可能导致性能比单线程情况更差。尝试确保每个线程写入的数据与其他线程的数据按缓存行的大小(通常约为64字节)分开。特别是,没有输出数组,如 [线程A,B,C,D,A,B,C,D ......]

  • Coarse-grained locking may overly serialize execution - that is, a lock may result in only one thread running at a time. You get all the overhead of multiple threads but none of the benefits. Try to reduce how long locks are held.
  • The same applies to overly frequent barriers and other synchronization structures. If the inner j loop completes quickly, you might spend most of your time in the barrier. Try to do more work between synchronization points.
  • If your code runs too quickly, there may be no time to migrate threads to other CPU cores. This usually isn't a problem unless you create a lot of very short-lived threads. Using thread pools, or simply giving each thread more work can help. If your threads run for more than a second or so each, this is unlikely to be a problem.
  • If your threads are working on a lot of shared read/write data, cache line bouncing may decrease performance. That said, although this often results in performance degradation, this alone is unlikely to result in performance worse than the single threaded case. Try to make sure the data that each thread writes is separated from other threads' data by the size of a cache line (usually around 64 bytes). In particular, don't have output arrays laid out like [thread A, B, C, D, A, B, C, D ...]

由于您没有显示您的代码,我在这里无法详细说明。

Since you haven't shown your code, I can't really speak in any more detail here.