concurrency - Java locking of elements inside array -
I have a dynamic array of hashtables
Do I use synchronize for each of them separately Can i do Synchronize (array [1]) {code ..}, synchronize (array [2]) {code ..}
thanks
You can definitely synchronize the object in a particular state in a certain position:
syn (arr [x]) {. ..}
However, be careful to make sure that you understand that it is doing what you want to do.
This particular object will lock referenced by arr [x] however, it does not buy you any thread-protection as access to the array - in other words, For example:
- When you are locking the object on [x], the second thread may still change potentially, which is in orange [x];
- If two threads arr arr arr place (either that hash map / object, or to set a new position), there will be a race condition.
I also agree with Ackappa - what you are doing is a little unusual, and this again the question is, "what do we need to do to do ? " Assuming that an array of hamppas is appropriate from the beginning?
Comments
Post a Comment