Quotes from Brian Goetz
It is far easier to design a class to be thread-safe than to retrofit it for thread safety later.
~ Brian Goetz
BazillionQuotes.com
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
BazillionQuotes.com
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
BazillionQuotes.com
Locking can guarantee both visibility and atomicity; volatile variables can only guarantee visibility.
~ Brian Goetz
BazillionQuotes.com
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
BazillionQuotes.com
When a field is declared volatile, the compiler and runtime are put on notice that this variable is shared and that operations on it should not be reordered with other memory operations. Volatile variables are not cached in registers or in caches where they are hidden from other processors, so a read of a volatile variable always returns the most recent write by any thread.
~ Brian Goetz
BazillionQuotes.com
The possibility of incorrect results in the presence of unlucky timing is so important in concurrent programming that it has a name: a race condition. A race condition occurs when the correctness of a computation depends on the relative timing or interleaving of multiple threads by the runtime; in other words, when getting the right answer relies on lucky timing.
~ Brian Goetz
BazillionQuotes.com
Debugging tip: For server applications, be sure to always specify the -server JVM command line switch when invoking the JVM, even for development and testing. The server JVM performs more optimization than the client JVM, such as hoisting variables out of a loop that are not modified in the loop; code that might appear to work in the development environment (client JVM) can break in the deployment environment (server JVM).
~ Brian Goetz
BazillionQuotes.com
Once an object escapes, you have to assume that another class or thread may, maliciously or carelessly, misuse it. This is a compelling reason to use encapsulation: it makes it practical to analyze programs for correctness and harder to violate design constraints accidentally.
~ Brian Goetz
BazillionQuotes.com
Accessing shared, mutable data requires using synchronization; one way to avoid this requirement is to not share. If data is only accessed from a single thread, no synchronization is needed. This technique, thread confinement, is one of the simplest ways to achieve thread safety. When an object is confined to a thread, such usage is automatically thread-safe even if the confined object itself is not.
~ Brian Goetz
BazillionQuotes.com
Good uses of volatile variables include ensuring the visibility of their own state, that of the object they refer to, or indicating that an important lifecycle event (such as initialization or shutdown) has occurred.
~ Brian Goetz
BazillionQuotes.com
immutability is not equivalent to simply declaring all fields of an object final. An object whose fields are all final may still be mutable, since final fields can hold references to mutable objects.
~ Brian Goetz
BazillionQuotes.com
To ensure thread safety, check-then-act operations (like lazy initialization) and read-modify-write operations (like increment) must always be atomic. We refer collectively to check-then-act and read-modify-write sequences as compound actions: sequences of operations that must be executed atomically in order to remain thread-safe.
~ Brian Goetz
BazillionQuotes.com
The java.util.concurrent.atomic package contains atomic variable classes for effecting atomic state transitions on numbers and object references. By replacing the long counter with an AtomicLong, we ensure that all actions that access the counter state are atomic.
~ Brian Goetz
BazillionQuotes.com
To preserve state consistency, update related state variables in a single atomic operation.
~ Brian Goetz
BazillionQuotes.com
With the exception of immutable objects, it is not safe to use an object that has been initialized by another thread unless the publication happens-before the consuming thread uses it.
~ Brian Goetz
BazillionQuotes.com
Every shared, mutable variable should be guarded by exactly one lock. Make it clear to maintainers which lock that is.
~ Brian Goetz
BazillionQuotes.com
Avoid holding locks during lengthy computations or operations at risk of not completing quickly such as network or console I/O.
~ Brian Goetz
BazillionQuotes.com
it is a common misconception that synchronized is only about atomicity or demarcating "critical sections". Synchronization also has another significant, and subtle, aspect: memory visibility. We want not only to prevent one thread from modifying the state of an object when another is using it, but also to ensure that when a thread modifies the state of an object, other threads can actually see the changes that were made.
~ Brian Goetz
BazillionQuotes.com
When used properly, threads can reduce development and maintenance costs and improve the performance of complex applications.
~ Brian Goetz
BazillionQuotes.com
always use the proper synchronization whenever data is shared across threads. 3.1.1.
~ Brian Goetz
BazillionQuotes.com
Stale data can cause serious and confusing failures such as unexpected exceptions, corrupted data structures, inaccurate computations, and infinite loops. [2]
~ Brian Goetz
BazillionQuotes.com
Locking is not just about mutual exclusion; it is also about memory visibility. To ensure that all threads see the most up-to-date values of shared mutable variables, the reading and writing threads must synchronize on a common lock.
~ Brian Goetz
BazillionQuotes.com
Just as tasks should have a cancellation policy, threads should have an interruption policy.
~ Brian Goetz
BazillionQuotes.com
