multithreading - Java volatile variable question -
I was thinking about the Java sensitization that the following code:
private Volatile list list; Private Finals Lock Lock = New RetreatLock (); Public Zero update (list newList) {ImmutableList L = New ImmutableList (). AddAll (newList); Lock.lock (); List = L; lock unlock(); } Public list () {return list; }
equals:
Personal volatile list list; Public Zero update (list newList) {ImmutableList L = New ImmutableList (). AddAll (newList); List = L; } Public list () {return list; } {/ Code>
Try {} Finally {} the block was left for brevity. I accept the ImmutableList class as a truly immutable data structure, which keeps its own data, Such as the google-collections provided in the library since the list variable is variable and basically it is happening that copy-on-the-fly, is it not safe to avoid using lock only?
In this very specific example, I think you will not make any locking on variable reassignment .
In general, I think that you are better by using one instead of the volatile variable because memory stability effects are the same and intentions are very clear.
Comments
Post a Comment