Multithreading Flashcards
Thread vs Runnable
you can implement Runnable and extend from another class as well
start() vs run()
start a Thread
vs
just execute run() method
synchronization of java blocks vs methods
synchronized method acquires a lock on the whole object.
This means no other thread can use any synchronized method
in the whole object while the method is being run by one thread.
vs
synchronized blocks acquires a lock in the object between
parentheses after the synchronized keyword. Meaning no other
thread can acquire a lock on the locked object until the synchronized block exits.
wait()/notify() usage
Object.wait() – to suspend a thread
Object.notify() – to wake a thread up
sleep() vs wait()
wait() – until call notify(), notifyAll() from object
sleep() – until at least time expire or call interrupt().
atomic operations
(internal implementation of atomic)
through CPU instructions, volatile and CAS
CAS operation
CAS operation (compare-and-swap):
memory location M
existing expected value: A
new value B
The CAS operation updates atomically the value in M to B,
but only if the existing value in M matches A, otherwise no action is taken.
If so - handle failure manually
volatile
should we rely on volatile?
it cannot be cached into the computer’s(microprocessor) cache memory by any thread
If there is a write operation going on a volatile variable, and suddenly a read operation is requested, it is guaranteed that the write operation will be finished prior to the read operation.
race condition is possible if previous value is needed to define next value
several threads could read same value
Thread local? What are they needed for?
variables that can only be read and written by the same thread
Does child thread see the value
of parent thread local?
If you mean InheritableThreadLocal
(extending ThreadLocal), then yes, each
child thread will have the initial default value
to be the same as the parent thread value.
But any changes by the child thread will be local to the child.
Deadlock definition
example
deadlock is a state in which each member of
a group of actions, is waiting for some other
member to release a lock
Livelock definition
example
special case of resource starvation where two
processes follow an algorithm for resolving a deadlock
that results in a cycle of different locked states because
each process is attempting the same strategy to avoid the lock.
Starvation definition
example
situation where a thread is unable to gain regular
access to shared resources and is unable to make progress.
This happens when shared resources are made unavailable
for long periods by “greedy” threads.
Race condition definition
exampple
when two or more threads can access
shared data and they try to change it at the same time
Executors
Types
ExecutorService can execute Runnable and Callable tasks
cachedThreadPool creates new threads as needed, but will reuse previously constructed threads when they are available
SingleThreadPool single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed.
FixedThreadPool reuses a fixed number of threads
If you want to process all submitted tasks in order of arrival, just use newFixedThreadPool(1)
If you want to optimize performance of big computation of recursive tasks, use ForkJoinPool or newWorkStealingPool
If you want to execute some tasks periodically or at certain time in future, use newScheduledThreadPool
Happens-before
When thread A enter sync for operation block and thread B is waiting. Then JVM guarantee A happens before B when thread B will know correct all values inialized by first thread before A operation. Other values JVM can reorder by hardware specific.
When A and B in the diffenent processes then JVM guarantee communication from A to B.
Transitivity of ““happens before””: A -> B and B -> C then A -> C
volatile vs atomic
Vollatile - one atomic operation. Atomics - few operations can be atomic.
volatile read vs getAndAdd(7)
Garbage definition plus example
unused objects or objects that are out of reach
Execution order
instruction-reordering-problems
In theory, the JVM could reorder
the instructions but the outcome of the
method has to remain correct.
it never reorders code around native instructions.
Atomicity of long and double assignment
operations
single write to a non-volatile long or double
value is treated as two separate writes:
one to each 32-bit half
volatile grants atomicity
Lock-free operations, how to create
lock-free implementation of field reassignment
The Lock-Free property guarantees that at least some thread is doing progress on its work.
atomic
What is Fork-Join?
provides tools to help speed up parallel processing
recursively breaking the task into smaller independent subtasks
results of all subtasks are recursively joined into a single result
ForkJoinPool
Worker threads can execute only one task at the time,
but the ForkJoinPool doesn’t create a separate thread
for every single subtask. Instead, each thread in the pool
has its own double-ended queue (or deque, pronounced deck) which stores tasks.
What is Phaser? (Java 7)
barrier on which the dynamic number of threads need to wait before continuing execution