Prev Source
Next Source

Java Concurrency in Practice

UnsafeSequence illustrates a common concurrency hazard called a race condition. Whether or not getNext returns a unique value when called from multiple threads, as required by its specification, depends on how the runtime interleaves the

Context switches—when the scheduler suspends the active thread temporarily so another thread can

When

program that consists entirely of thread-safe classes may not be thread-safe, and a thread-safe program may contain classes that are not thread-safe.

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

Whether another thread actually does something with a published reference doesn’t really matter, because the risk of misuse is still

If you are tempted to register an event listener or start a thread from a constructor, you can avoid the improper construction by using a private constructor and a public factory method, as shown in SafeListener in Listing 3.8


Date
April 18, 2023