Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COActually, my example wasn't really correct. I didn't mean to mark _mutableObject as final. I don't think this locking will work *IF* _mutableObject in the parent object is non-final. If you pass that value as a variable into the helper method, and then you change _mutableObject to point to something else in the parent class, when the helper method runs it is no longer holding onto the correct reference for _mutableObject. The lock will still lock, but you will be "protecting" a stale value of _mutableObject. That is really what my question was supposed to point out.
      singulars
    2. COI will accept your answer assuming that this will only work if _mutableObject is 'final' in the parent class. The _lock object always needs to be declared final, no matter which version of my example you use. In fact, I shouldn't have created my example to include a "mutable" object. I should have just described that I wanted to protected a non-final variable, call it "private Object _object". I want the lock to protect that value changing. If the example is changed to that, I don't think you can pass the locking off to a helper method, because helper method gets stale _object reference.
      singulars
    3. CO@Android Dev An object used for a lock *object* should be stable and unique for each mutually exclusive synchronization desired. The variable (or object) being protected by the synchronization can change (the synchronization context will ensure the memory barriers and happens-before guarantees as long as all such synchronizations operate upon the same lock *object*). It's generally a bad idea to swap an object used for a lock (the lock *object* can be mutated and this will not affect it's usage with `synchronized` -- the importance is that the *same object* is used for synchronization.)
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload