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