使用Java中的Threads需要多长时间的操作?

时间:2022-06-02 20:56:55

I program Java and I want to make my multithread programs work faster.

我编写Java程序,我想让我的多线程程序更快地运行。

I know that setting/reading boolean variable is faster than locking/unlocking of mutex.
Locking/unlocking is faster than making thread sleep or wake.
And sleep/wake pair is faster than creating absolutely new thread.

我知道设置/读取布尔变量比锁定/解锁互斥锁更快。锁定/解锁比线程休眠或唤醒更快。睡眠/唤醒对比创建绝对新线程更快。

I know that this processes are specific for different hardware and OS. But is there any estimations of lead time?
I look for something like "10 times to sleep/wake lasts as long as 1 time to create new thread".

我知道这个过程特定于不同的硬件和操作系统。但有没有预测时间的估计?我寻找类似“10次睡眠/唤醒持续长达1次创建新线程”的东西。

1 个解决方案

#1


I want to make my multithread programs work faster

我想让我的多线程程序更快地运行

The best way to focus on this is to Memory and CPU profile your application. I suggest memory profiling first as this often has the quickest wins. As you didn't mention it, perhaps you hadn't though about it but you can be losing 50+% of your performance to creating and cleaning up objects if you are not careful.

关注此问题的最佳方法是对应用程序的内存和CPU进行配置。我建议首先进行内存分析,因为这通常会获得最快的胜利。正如你没有提到的那样,也许你没有关于它,但如果你不小心,你可能会失去50%以上的表现来创建和清理对象。

After you have cleaned up your memory consumption, you can do a CPU profiling. This is useful but sometimes harder to see how you can improve it without restructuring your application. There is a good chance this is what you need to do to reduce consumption. A common question for me is; do I know that adding more threads really helps. If I don't know this because I measure it, it could equally be hurting. You might be amazed how much you can speed up a multi-threaded application, in particular it's worse case latencies, by taking out threads.

清理内存消耗后,可以进行CPU分析。这很有用,但有时候很难看到如何在不重构应用程序的情况下改进它。这很有可能是您需要做的减少消费的方法。对我来说一个常见的问题是;我是否知道添加更多线程确实有帮助。如果我不知道这个,因为我测量它,它同样可能会伤害。您可能会惊讶于您可以通过取出线程来加速多线程应用程序,尤其是更糟糕的情况延迟。

Finally if you want to go to a lower level, try Flight Recorder in Java Mission Control. This is a lower level view. Some think this is better than a profiler, but these people tend to write efficient code in the first place. I think most developers should start with a profiler.

最后,如果您想要更低级别,请尝试使用Java Mission Control中的Flight Recorder。这是一个较低级别的视图。有些人认为这比分析器更好,但这些人倾向于首先编写高效的代码。我认为大多数开发人员应该从分析器开始。

I know that this processes are specific for different hardware and OS. But is there any estimations of lead time?

我知道这个过程特定于不同的硬件和操作系统。但有没有预测时间的估计?

They also vary by how much you use them and in a multi-threaded context, how much does one operation slow down another thread. i.e. if you obtain a lock which is;

它们也会因您使用它们的程度而异,在多线程上下文中,一个操作会减慢另一个线程的速度。即如果你获得一把锁;

  • only ever used by one thread it is pretty cheap.
  • 只有一个线程使用它很便宜。

  • rarely shared by another thread it costs significantly more
  • 很少由另一个线程共享,它的成本要高得多

  • is actually being held by another thread it can take much longer.
  • 实际上是由另一个线程持有它可能需要更长的时间。

How much time a lock takes depends on how it is used.

锁定需要多长时间取决于它的使用方式。

Another problem is a questions of what is the bottle neck. If you bottle neck is CPU consumption using more threads can help to spread the load but if the problem is CPU to memory bandwidth of L3 cache consumption, more threads can be slower.

另一个问题是什么是瓶颈问题。如果你的瓶颈是CPU消耗使用更多的线程可以帮助分散负载,但如果问题是CPU到内存带宽的L3缓存消耗,更多的线程可以更慢。

In short, too much depends on what you are doing, find the bottleneck and reduce it, then find the next bottleneck etc.

简而言之,太多取决于你在做什么,找到瓶颈并减少它,然后找到下一个瓶颈等。

#1


I want to make my multithread programs work faster

我想让我的多线程程序更快地运行

The best way to focus on this is to Memory and CPU profile your application. I suggest memory profiling first as this often has the quickest wins. As you didn't mention it, perhaps you hadn't though about it but you can be losing 50+% of your performance to creating and cleaning up objects if you are not careful.

关注此问题的最佳方法是对应用程序的内存和CPU进行配置。我建议首先进行内存分析,因为这通常会获得最快的胜利。正如你没有提到的那样,也许你没有关于它,但如果你不小心,你可能会失去50%以上的表现来创建和清理对象。

After you have cleaned up your memory consumption, you can do a CPU profiling. This is useful but sometimes harder to see how you can improve it without restructuring your application. There is a good chance this is what you need to do to reduce consumption. A common question for me is; do I know that adding more threads really helps. If I don't know this because I measure it, it could equally be hurting. You might be amazed how much you can speed up a multi-threaded application, in particular it's worse case latencies, by taking out threads.

清理内存消耗后,可以进行CPU分析。这很有用,但有时候很难看到如何在不重构应用程序的情况下改进它。这很有可能是您需要做的减少消费的方法。对我来说一个常见的问题是;我是否知道添加更多线程确实有帮助。如果我不知道这个,因为我测量它,它同样可能会伤害。您可能会惊讶于您可以通过取出线程来加速多线程应用程序,尤其是更糟糕的情况延迟。

Finally if you want to go to a lower level, try Flight Recorder in Java Mission Control. This is a lower level view. Some think this is better than a profiler, but these people tend to write efficient code in the first place. I think most developers should start with a profiler.

最后,如果您想要更低级别,请尝试使用Java Mission Control中的Flight Recorder。这是一个较低级别的视图。有些人认为这比分析器更好,但这些人倾向于首先编写高效的代码。我认为大多数开发人员应该从分析器开始。

I know that this processes are specific for different hardware and OS. But is there any estimations of lead time?

我知道这个过程特定于不同的硬件和操作系统。但有没有预测时间的估计?

They also vary by how much you use them and in a multi-threaded context, how much does one operation slow down another thread. i.e. if you obtain a lock which is;

它们也会因您使用它们的程度而异,在多线程上下文中,一个操作会减慢另一个线程的速度。即如果你获得一把锁;

  • only ever used by one thread it is pretty cheap.
  • 只有一个线程使用它很便宜。

  • rarely shared by another thread it costs significantly more
  • 很少由另一个线程共享,它的成本要高得多

  • is actually being held by another thread it can take much longer.
  • 实际上是由另一个线程持有它可能需要更长的时间。

How much time a lock takes depends on how it is used.

锁定需要多长时间取决于它的使用方式。

Another problem is a questions of what is the bottle neck. If you bottle neck is CPU consumption using more threads can help to spread the load but if the problem is CPU to memory bandwidth of L3 cache consumption, more threads can be slower.

另一个问题是什么是瓶颈问题。如果你的瓶颈是CPU消耗使用更多的线程可以帮助分散负载,但如果问题是CPU到内存带宽的L3缓存消耗,更多的线程可以更慢。

In short, too much depends on what you are doing, find the bottleneck and reduce it, then find the next bottleneck etc.

简而言之,太多取决于你在做什么,找到瓶颈并减少它,然后找到下一个瓶颈等。