Java Multi-Threading Flashcards

1
Q

Data Race

A

Two or more threads in a single process access the same memory location concurrently and, at least one of the accesses is for writing, and the threads are not using any exclusive locks to control their accesses to that memory

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

Deadlock

A

Two threads hold locks on different resources, each waiting indefinitely for the other to release its lock

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

Synchronized method

A

A method that can only be executed by a single thread at a time

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

Method ‘lock’

A

enclosed in a try/finally because if the ‘try’ code throws an exception, the lock must be unlocked or it will block other threads forever. Only one thread can lock/unlock at a time (for each lock)

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

Semaphore

A

Controls access to shared resources through the use of a counter. If counter > 0, access is allowed, otherwise denied. If counter is < 0, thread will be blocked until a different thread releases their permit.

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

Thread vs Process

A

Thread: smallest units of the particular process. Has the ability to execute different parts of the program at the same time.
Process: refers to a program that is in execution

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

User Thread vs Daemon Thread

A

User thread: JVM waits for user threads to finish their tasks before termination. Created by user to execute tasks concurrently. High-priority, required to run in foreground
Daemon thread: JVM does not wait for daemon threads to finish their tasks before termination. Created by JVM. Low-priority used for garbage collection, releasing memory of unused objects, etc.

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

Wait() vs Sleep() methods

A

Wait - causes current thread to wait and sleep. Releases the lock for inter-thread communication.
Sleep - pauses or stops the execution of the current thread. Does not release lock.

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

Start() vs Run()

A

Start - starts the execution of a newly created thread. this newly created thread executes the task that is kept in the run method
Run - starts the execution of the same thread. No new thread is created

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

Join()

A

pauses the execution of a thread until thread 1 in thread.join(thread1) is dead or completed.

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

Garbage Collection

A

automatic memory management. Three phases - mark, delete, compact/copy. A garbage collector finds objects that are no longer required by the program and deletes them to free up memory space

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

Volatile variable

A

Allows a field to become visible to all readers (threads) after a write operation completes on it. Without volatile, some threads could see some non-updated value.

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

Synchronized Block

A

Locks part of the program between parentheses after the synchronized keyword. Preferred to synchronized method because it doesn’t lock up the whole method.

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

Livelock

A

state of threads changes between one another without making any progress. Threads are not blocked but their execution is stopped due to the unavailability of resources

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

BlockingQueue

A

blocks a thread from trying to insert into a full queue until another thread takes from it, and vice versa

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

CyclicBarrier

A

enables a set of threads to wait for each other until they reach a common execution point, then lets them all continue

17
Q

CountDownLatch

A

enables main threads to wait until mandatory operations are performed and completed by other threads.

18
Q

What is multi-threading?

A

a process of executing multiple threads simultaneously. Consumes less memory. Threads share the same address space, thread is lightweight, cost of communication between processes is low

19
Q

What is a thread?

A

a subprocess that shares the resources of a process.

20
Q

5 states of a Thread

A

New, runnable, running, waiting/blocked, dead/terminated

21
Q

Functions used to perform inter-thread communication?

A

notify, wait, notifyAll

22
Q

Context Switching

A

a feature through which the current state of a thread is saved for it to be restored and executed later. This allows multiple processes to share the same CPU

23
Q

How to avoid deadlock?

A

avoid nested thread locks and provide locks to only one thread at a time. Use thread.join(thread2)

24
Q

Lock-free data structure

A

allows individual threads to starve but guarantees at least one thread always making progress. All wait-free algorithms are lock-free

25
Q

Four phases of lock-free algorithms

A

complete one’s own operation, assist an obstruction operation, abort an obstructing operation, wait.

26
Q

Atomic variable and example

A

a = 28 (a an int) is an atomic operation but a++ is not because it requires a read of the value ‘a’, an incrementation, and then a write to ‘a’. Atomic operation incrementAndGet(i) solves this dilemma

27
Q

Avoid data-race without java.util.concurrent

A

synchronized method/statement. Each object in Java is associated with a monitor which a thread can lock or unlock. Only one thread at a time can hold a lock on a monitor.