Multithreading 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

synchronization of java blocks vs methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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

17
Q

volatile vs atomic

A

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

18
Q

Garbage definition plus example

A

unused objects or objects that are out of reach

19
Q

Execution order
instruction-reordering-problems

A

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.

20
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

21
Q

Lock-free operations, how to create
lock-free implementation of field reassignment

A

The Lock-Free property guarantees that at least some thread is doing progress on its work.
atomic

22
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

23
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.

24
Q

What is Phaser? (Java 7)

A

barrier on which the dynamic number of threads need to wait before continuing execution

25
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.

26
Q

work-stealing algorithm

A

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

27
Q

thread pools

A

ForkJoinPool
FixedThreadPool
CachedThreadPool
SingleThreadExecutor

28
Q

CountDownLatch

A

thread which calls CountDownLatch.await() will wait until count reaches zero or it’s interrupted by another thread.
other threads are required to count down by calling CountDownLatch.countDown() once they are completed or ready.

29
Q

CyclicBarier

A

synchronizer that allows a set of threads to wait for each other
to reach a common execution point, also called a barrier.

30
Q

access barrier

A

A barrier for a group of threads or processes
in the source code means any thread/process
must stop at this point and cannot proceed
until all other threads/processes reach this barrier.

31
Q

Exchanger

A

A synchronization point at which threads can pair and swap elements within pairs.

32
Q

Monitor

A

mechanism to control concurrent access to an object
prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time.

33
Q

Semaphore

A

to limit the number of concurrent threads accessing a specific resource.

34
Q

ForkJoinPool vs Executor Framework

A

1 accepts ForkJoinTask vs FutureTask
2 - ForkJoinPool uses work stealing pattern
executors

35
Q

Mutex

A

acts similarly to a binary semaphore