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 co开发者_如何学运维de?
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.
精彩评论