Compound actions on shared state, such as incrementing a hit counter (read-modify-write) or lazy initialization (check-then-act), must be made atomic to avoid race conditions. Holding a lock for the entire duration of a compound action can make that compound action atomic. However, just wrapping the compound action with a synchronized block is not sufficient; if synchronization is used to coordinate access to a variable, it is needed everywhere that variable is accessed. Further, when using locks to coordinate access to a variable, the same lock must be used wherever that variable is accessed.
Brian GoetzTag: programming java concurrency
Locking can guarantee both visibility and atomicity; volatile variables can only guarantee visibility.
Brian GoetzTag: programming java concurrency
Just as it is a good practice to make all fields private unless they need greater visibility, it is a good practice to make all fields final unless they need to be mutable.
Brian GoetzTag: programming java concurrency
« prima precedente
Pagina 2 di 2.
Data privacy
Imprint
Contact
Diese Website verwendet Cookies, um Ihnen die bestmögliche Funktionalität bieten zu können.