mousey
mousey

Reputation: 11901

Question regarding synchronization

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

Upvotes: 4

Views: 175

Answers (5)

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

It's simply not true that "synchronization slows down programs" - it only does when the synchronized actions are done very frequently, or when you actually have a lot of threads contending for them. For most applications, neither is true.

Also, some kinds of concurrent operations can be implemented safely without synchronization by using clever data structures or hardware primitives. Examples:

Upvotes: 1

fastcodejava
fastcodejava

Reputation: 41087

You might want to synchronize the block of code rather than the whole method. Without it, you are risking whole lot more!

Upvotes: 0

Marcus Adams
Marcus Adams

Reputation: 53830

Saying that a synchronization construct slows down execution is like saying that a parachute slows down a skydiver. Going without will be faster, but that's not exactly the point. Synchronization serves a purpose.

To improve the speed of execution, simply apply synchronization properly.

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

Upvotes: 7

crazyscot
crazyscot

Reputation: 11989

  • Profile your code, find out where the real bottlenecks lie.
  • Carefully re-analyse your critical regions. It's very easy to apply synchronization too broadly.
  • Sometimes changing algorithm can lead to a completely different synchronization profile. This doesn't always have a positive effect though!

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272217

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

The early JVMs suffered a penalty when using synchronisation. However that situation has improved vastly over the years. I wouldn't worry about a performance penalty when synchronising. There will be many more candidates for optimisations.

Upvotes: 0

Related Questions