If i come across a situation where i will have to make each and every method in my Java program synchronized, will that affect the performance of my code?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it will influence the performance.
If your application is mostly single-threaded, then the impact will be very small, because uncontested lock acquisition is very fast (on modern JVMs such as HotSpot).
If your application is heavily multi-threaded and multiple threads access the same objects concurrently, then the impact will be larger.
Note that having every single method synchronized does not guarantee that your code is thread-safe, you can still easily get race conditions.