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.
Say I have a public method1 calling a private method2 , I use a
Share
If
method2is called only bymethod1, then you don’t have to use a lock inmethod2. The one lock inmethod1is enough.When a thread class
method1, it will acquire the lock, callmethod2,method2won’t be executing concurrently by another thread since the other thread should have calledmethod1first, and in such a case,method1would fail to acquire the lock.