Is there any downside to making every one of your methods synchronized in Android?
Is there any downside to making every one of your methods synchronized in Android?
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 end up taking out locks when you don’t really want them. It won’t give you thread safety for free – it’ll just slow down your code and make it more likely that you’ll run into deadlocks due to taking out too many locks.
You need to think about thread safety and synchronization explicitly. I usually make most classes not thread-safe, and try to limit the number of places where I think about threading.
The “make everything synchronized” approach is a common one in what I think of as the four stages of threading awareness for developers:
Most experienced developers are in stage 3 as far as I can tell – with different levels of ease within it, of course. Using immutability, higher-level abstractions instead of the low-level primitives etc helps a lot – but ultimately you’re likely to have to think a fair amount whenever you’ve got multiple threads which need to share state.