Chapter 2 Flashcards
synchronization
It includes the use of volatile variables, explicit locks and atomic variables
Encapsulation and data hiding
Help you to create thread-safe classes.
Thread safe class
A class is thread-safe when it continues to behave correctly when accessed from multiple threads regardles of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other cordination on the part of the calling code.
Fixing broken thread access
-Dont share the state variable across threads;
-Make the state variable immutable
-Use synchronization whenever accessing the state variable
Note
When designing thread-safe classes, good object-oriented techniques — encapsulation, immutability, and clear specification of invariants—are
your best friends.
Thread safe class
Thread safe classes encapsulate any needed synchronization so that clients nedd to provide their own.
Stateless object
Stateless objects are always thread-safe