ThreadLocal Storage
The ThreadLocal class allows values to be stored inside of the currently
running Thread object. For example, imagine tracing an HTTP request that
starts in a servlet and traverses through a service. Rather than pass a transaction
id as an argument in every method, you could instead store the id in
a ThreadLocal object and retrieve it statically along the way.
ThreadLocal
variables reduce the verbosity of methods, but care must
be taken to ensure that a value is removed if the thread is returned
to a thread pool.
Atomic Variables
Java introduced several variables with built-in concurrency such
as AtomicInteger, AtomicLong, and AtomicBoolean. For example,
an AtomicInteger could replace and even outperform our Counter class
because it’s implemented with native method calls. A native method call
executes outside of the JVM in a platform-dependent programming language.
Questions
What is the lifecycle of a Thread?
Why is synchronization necessary on shared resources?
What is used as a lock for synchronized static and synchronized non-
static methods?
What would happen if two different threads hit two synchronized non-static
methods on the same object simultaneously?
What would happen if two different threads hit a synchronized static method and
synchronized non-static method on the same object simultaneously?
What one thing does the volatile keyword guarantee about a variable?
What two things does the synchronize keyword guarantee about a block of code?
What are some built-in concurrent data structures?
What is the executor framework?
What is a ThreadLocal variable?
What are atomic variables?
|