I guess @synchronized blocks are not object dependent but thread dependent…right? In that case why do we pass self?
I guess @synchronized blocks are not object dependent but thread dependent…right? In that case
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.
@synchronizedis a construct provided by the language to create synchronized scopes. As it would be highly inefficient to use a simple global shared mutex, and thus serializing every single@synchronizedscope in the application, the language allows us to specify a synchronization point.Then it’s up to the developer(s) to decide which synchronization points are appropriate for the task.
On an instance method, using self is common: the instance is the synchronization point. The
@synchronized(self)scope can be called on any number of instances, but only once for a given instance. Every@synchronized(self)scope will be serialized for a given instance.Of course, you are free to use another synchronization point if you want to do so. You can use the class (
@synchronized(self.class)) or anything else that suits your needs.