Java Concurrency in Practice Quotes

2,896 ratings, 4.48 average rating, 138 reviews
Open Preview
Java Concurrency in Practice Quotes
Showing 31-60 of 71
“A common mistake that can let the this reference escape during construction is to start a thread from a constructor. When an object creates a thread from its constructor, it almost always shares its this reference with the new thread, either explicitly (by passing it to the constructor) or implicitly (because the Thread or Runnable is an inner class of the owning object). The new thread might then be able to see the owning object before it is fully constructed. There's nothing wrong with creating a thread in a constructor, but it is best not to start the thread immediately. Instead, expose a start or initialize method that starts the owned thread. (See Chapter 7 for more on service lifecycle issues.) Calling an overrideable instance method (one that is neither private nor final) from the constructor can also allow the this reference to escape. If”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Publishing an object also publishes any objects referred to by its nonprivate fields. More generally, any object that is reachable from a published object by following some chain of nonprivate field references and method calls has also been published. From”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Publishing an object means making it available to code outside of its current scope, such as by storing a reference to it where other code can find it, returning it from a nonprivate method, or passing it to a method in another class. In many situations, we want to ensure that objects and their internals are not published. In other situations, we do want to publish an object for general use, but doing so in a thread-safe manner may require synchronization. Publishing internal state variables can compromise encapsulation and make it more difficult to preserve invariants; publishing objects before they are fully constructed can compromise thread safety. An object that is published when it should not have been is said to have escaped. Section 3.5 covers idioms for safe publication; right now, we look at how an object can escape. The”
― Java Concurrency in Practice
― Java Concurrency in Practice
“A special case of thread confinement applies to volatile variables. It is safe to perform read-modify-write operations on shared volatile variables as long as you ensure that the volatile variable is only written from a single thread. In this case, you are confining the modification to a single thread to prevent race conditions, and the visibility guarantees for volatile variables ensure that other threads see the most up-to-date value. Because”
― Java Concurrency in Practice
― Java Concurrency in Practice
“common application of thread confinement is the use of pooled JDBC (Java Database Connectivity) Connection objects. The JDBC specification does not require that Connection objects be thread-safe.[9] In typical server applications, a thread acquires a connection from the pool, uses it for processing a single request, and returns it. Since most requests, such as servlet requests or EJB (Enterprise JavaBeans) calls, are processed synchronously by a single thread, and the pool will not dispense the same connection to another thread until it has been returned, this pattern of connection management implicitly confines the Connection to that thread for the duration of the request. [9]”
― Java Concurrency in Practice
― Java Concurrency in Practice
“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”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Do not allow the this reference to escape during construction.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“You can use volatile variables only when all the following criteria are met: Writes to the variable do not depend on its current value, or you can ensure that only a single thread ever updates the value; The variable does not participate in invariants with other state variables; and Locking is not required for any other reason while the variable is being accessed.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“However, we do not recommend relying too heavily on volatile variables for visibility; code that relies on volatile variables for visibility of arbitrary state is more fragile and harder to understand than code that uses locking.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Yet accessing a volatile variable performs no locking and so cannot cause the executing thread to block, making volatile variables a lighter-weight synchronization mechanism than synchronized.[5]”
― Java Concurrency in Practice
― Java Concurrency in Practice
“The Java language also provides an alternative, weaker form of synchronization, volatile variables, to ensure that updates to a variable are propagated predictably to other threads. 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. A”
― Java Concurrency in Practice
― Java Concurrency in Practice
“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.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“The Java Memory Model requires fetch and store operations to be atomic, but for nonvolatile long and double variables, the JVM is permitted to treat a 64-bit read or write as two separate 32-bit operations. If the reads and writes occur in different threads, it is therefore possible to read a nonvolatile long and get back the high 32 bits of one value and the low 32 bits of another.[3] Thus, even if you don't care about stale values, it is not safe to use shared mutable long and double variables in multithreaded programs unless they are declared volatile or guarded by a lock. [3]”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Stale data can cause serious and confusing failures such as unexpected exceptions, corrupted data structures, inaccurate computations, and infinite loops. [2]”
― Java Concurrency in Practice
― Java Concurrency in Practice
“always use the proper synchronization whenever data is shared across threads. 3.1.1.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“in the absence of synchronization, the Java Memory Model permits the compiler to reorder operations and cache values in registers, and permits CPUs to reorder operations and cache values in processor-specific caches.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“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.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Avoid holding locks during lengthy computations or operations at risk of not completing quickly such as network or console I/O.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Every shared, mutable variable should be guarded by exactly one lock. Make it clear to maintainers which lock that is.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Acquiring the lock associated with an object does not prevent other threads from accessing that object—the only thing that acquiring a lock prevents any other thread from doing is acquiring that same lock. The fact that every object has a built-in lock is just a convenience so that you needn't explicitly create lock objects. [9] It is up to you to construct locking protocols or synchronization policies that let you access shared state safely, and to use them consistently throughout your program. [9]”
― Java Concurrency in Practice
― Java Concurrency in Practice
“When a thread requests a lock that is already held by another thread, the requesting thread blocks. But because intrinsic locks are reentrant, if a thread tries to acquire a lock that it already holds, the request succeeds. Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“(Static synchronized methods use the Class object for the lock.)”
― Java Concurrency in Practice
― Java Concurrency in Practice
“A synchronized block has two parts: a reference to an object that will serve as the lock, and a block of code to be guarded by that lock.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“To preserve state consistency, update related state variables in a single atomic operation.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“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.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“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.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“The most common type of race condition is check-then-act, where a potentially stale observation is used to make a decision on what to do next. [4]”
― Java Concurrency in Practice
― Java Concurrency in Practice
“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.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“Stateless objects are always thread-safe.”
― Java Concurrency in Practice
― Java Concurrency in Practice
“A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.”
― Java Concurrency in Practice
― Java Concurrency in Practice