user496949
user496949

Reputation: 86055

Do I need to lock the dependent private method?

Say I have a public method1 calling a private method2, I use a lock to ensure the thread safety of method1, do I need to use a lock on method2? method2 is only being called through method1.

Upvotes: 1

Views: 92

Answers (1)

insumity
insumity

Reputation: 5459

If method2 is called only by method1, then you don't have to use a lock in method2. The one lock in method1 is enough.

When a thread class method1, it will acquire the lock, call method2, method2 won't be executing concurrently by another thread since the other thread should have called method1 first, and in such a case, method1 would fail to acquire the lock.

Upvotes: 2

Related Questions