Immutable objects are simple. They can only be in one state, which is carefully controlled by the constructor. One of the most difficult elements of program design is reasoning about the possible states of complex objects. Reasoning about the state of immutable objects, on the other hand, is trivial.

Immutable objects are also safer. Passing a mutable object to untrusted code, or otherwise publishing it where untrusted code could find it, is dangerous — the untrusted code might modify its state, or, worse, retain a reference to it and modify its state later from another thread. On the other hand, immutable objects cannot be subverted in this manner by malicious or buggy code, so they are safe to share and publish freely without the need to make defensive copies.

Brian Goetz

Mots clés programming java concurrency



Aller à la citation


Whenever more than one thread accesses a given state variable, and one of them might write to it, they all must coordinate their access to it using synchronization.

Brian Goetz

Mots clés programming java concurrency



Aller à la citation


It is far easier to design a class to be thread-safe than to retrofit it for thread safety later.

Brian Goetz

Mots clés programming java concurrency



Aller à la citation


Sometimes abstraction and encapsulation are at odds with performance — although not nearly as often as many developers believe — but it is always a good practice first to make your code right, and then make it fast.

Brian Goetz

Mots clés programming java concurrency



Aller à la citation


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 Goetz

Mots clés programming java concurrency



Aller à la citation


Locking can guarantee both visibility and atomicity; volatile variables can only guarantee visibility.

Brian Goetz

Mots clés programming java concurrency



Aller à la citation


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 Goetz

Mots clés programming java concurrency



Aller à la citation


If you believe there is always a better way, take the next step in exploring your potential with ZaranTech

Alok Kumar

Mots clés java sap business-analysis dot-net online-training zarantech



Aller à la citation


« ; premier précédent
Page 2 de 2.


©gutesprueche.com

Data privacy

Imprint
Contact
Wir benutzen Cookies

Diese Website verwendet Cookies, um Ihnen die bestmögliche Funktionalität bieten zu können.

OK Ich lehne Cookies ab