MultithreadingPart2 Flashcards

1
Q

Thread vs Runnable

A

you can implement Runnable and extend from another class as well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

start() vs run()

A

start a Thread
vs
just execute run() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

wait()/notify() usage

A

Object.wait() – to suspend a thread
Object.notify() – to wake a thread up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

sleep() vs wait()

A

wait() – until call notify(), notifyAll() from object
sleep() – until at least time expire or call interrupt().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

atomic operations
(internal implementation of atomic)

A

through CPU instructions, volatile and CAS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

CAS operation

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

volatile
should we rely on volatile?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Thread local? What are they needed for?

A

variables that can only be read and written by the same thread

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does child thread see the value
of parent thread local?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Deadlock definition
example

A

deadlock is a state in which each member of
a group of actions, is waiting for some other
member to release a lock

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Livelock definition
example

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Starvation definition
example

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Race condition definition
exampple

A

when two or more threads can access
shared data and they try to change it at the same time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Executors
Types

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Happens-before

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

volatile vs atomic

A

Vollatile - one atomic operation. Atomics - few operations can be atomic.
volatile read vs getAndAdd(7)

17
Q

Garbage definition plus example

A

unused objects or objects that are out of reach

18
Q

Atomicity of long and double assignment
operations

A

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

19
Q

What is Fork-Join?

A

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

20
Q

ForkJoinPool

A

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.

21
Q

Reentrant lock

A

As the name says, ReentrantLock allow
threads to enter into lock on a resource
more than once. When the thread first enters
into lock, a hold count is set to one.
Before unlocking the thread can re-enter into
lock again and every time hold count is
incremented by one. For every unlock request,
hold count is decremented by one and when
hold count is 0, the resource is unlocked.

22
Q

work-stealing algorithm

A

free threads try to “steal” work from deques of busy threads.