Question about synchronization

Using synchronization slows down program execution. Is there a way to improve execution speed?

+4
source share
5 answers
  • Profile your code, find out where the real bottlenecks lie.
  • Reanalyze your critical regions carefully. It is very simple to apply synchronization too widely.
  • Sometimes a changing algorithm can lead to a completely different synchronization profile. This does not always have a positive effect, though!
0
source

To say that the synchronization design slows down the execution, it is like a parachute slows down the operation of the paratrooper. Going without will be faster, but that's not entirely accurate. Synchronization serves the purpose.

To speed up execution, just do the right synchronization.

For example, using the Producer / Consumer design pattern can help you reduce the number of synchronization constructs needed in your code.

+7
source

It is simply not true that “synchronization slows down programs” —this happens only when synchronized actions are performed very often or when you actually have many threads competing for them. For most applications this is not the case.

In addition, some types of parallel operations can be safely implemented without synchronization using smart data structures or hardware primitives. Examples:

+1
source

Have you measured how much (if any) the slowdown?

Early JVMs suffered when using synchronization. However, over the years this situation has improved significantly. I would not worry about a performance penalty when synchronizing. There will be many more candidates for optimization.

0
source

You might want to synchronize a block of code, not the whole method. Without this, you risk a lot more!

0
source

Source: https://habr.com/ru/post/1311556/


All Articles