Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question regarding synchronization

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

like image 587
mousey Avatar asked Feb 27 '23 22:02

mousey


2 Answers

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.

like image 164
Marcus Adams Avatar answered Mar 12 '23 15:03

Marcus Adams


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:

  • ConcurrentHashMap
  • CopyOnWriteArrayList
  • AtomicInteger
like image 27
Michael Borgwardt Avatar answered Mar 12 '23 15:03

Michael Borgwardt