Java multithreading and synchronized: performance? | Sololearn: Learn to code for FREE!

+1

Java multithreading and synchronized: performance?

I know that the keyword 'synchronized' locks the object to the thread and release it after it's done. While I am aware that this is to prevent the acess to the instance variables in the class object at the same time, I wonder if this beats the purpose of using multithreads?..is it still faster to use multithreads with the synchronized keywords. Maybe I am missing some points here. Could anyone explain this please??

8/25/2017 10:03:24 AM

minsik kim

2 Answers

New Answer

+2

I don't know much about it but here's my perspective; It's is related with thread safety measures, a synchronized object is locked to prevent race condition, if a value can be accessed by codes running on different threads then the value modification must be arranged so that the previous thread is allowed to complete its operations on the object without getting interrupted, once it has done its business; the lock is removed, and the next thread can work with it. I don't think that beats the multithreading concept, instead, it provides more accuracy and safety. I guess if it comes to performance vs safety everyone has their own reasons and purpose, but personally I prefer safety over minor performance loss.

0

Multi-threaded programs may often come to a situation where multiple threads try to access the same resources and finally produce erroneous and unforeseen results. So it needs to be made sure by some synchronization method that only one thread can access the resource at a given point of time.