Concurrency/Threading Flashcards

1
Q

What is concurrency?

A

Concurrency is the ability to perform multiple processes out-of-order or in partial order, including the potential interaction between them.

In other words, it’s our ability to safely run multiple processes in parallel.

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

What is a thread?

A

A thread is almost like a smaller process within a process. They are each allotted their own stack and registers just as if they were separate processes.

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

What is multithreading?

A

Multithreading is the ability of the operating system to run threads in parallel.

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

What is the difference between virtual and physical threading?

A

Virtual threading in a single processor is where the operating system uses techniques such as fine grain threading to simulate multiple processes running at once.

Physical threading in a multi-core system is where the operating system assigns each thread a core.

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

What is meant by “non-determinism” in threading?

A

Non-determinism means that the order of task execution is impossible to be determined. In other words, each thread will be unable to tell which program is starting or finishing last, and will not know if it itself is first or last.

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

Why do we call start() and not run() in Java?

A

run() is the interface identifier for the method that is to be executed. Hence, if you call run() directly, it will simply run on the thread it is executed on, and not a new thread.

start() starts a new thread and begins executing run() on that new thread.

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

How can you stop a thread?

A

Threads will automatically stop and clean themselves up once they hit the end of the run() block, or if it is returned.

If you want to continue looping infinitely until stated otherwise, you can use isInterrupted() as a while flag to continue executing until a controlling parent thread interrupts or stops it.

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

What are race conditions?

A

A race condition is caused when two threads try to access/change data at the same time.

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

Why do race conditions happen?

A

Because threading is non-deterministic, if both threads try and do something different to the data, it will be random (or effectively random) which thread starts first, making the outcome (effectively) random each time it is ran.

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

What does the “synchronized” keyword do in Java?

A

The synchronized keyword places a ‘lock’ on the method whenever a thread accesses it. A method that has a ‘lock’ on it may not be accessed by another thread. This ‘lock’ remains until the method or synchronized block has finished executing.

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

What is the condition caused when no member of a group of entities can proceed because each entity is waiting for another member in the group?

A

Deadlock

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

What is the condition caused when a thread cannot gain regular access to shared resources because another thread is hogging the memory access?

A

Starvation

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

What is the condition caused when two threads are too busy responding to each other to carry out their tasks?

A

Livelock

An example because this one is complicated: two people bump into each other in a hallway, so they walk back to where they started and then walk back down the same hallway, repeating this in a loop.

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

Which out of wait() and sleep() will only wake up when interrupted?

A

sleep().

It cannot be woken up using a notify() or notifyAll().

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

Which out of wait() and sleep() gives up the thread’s allotted time-slice whenever they are called?

A

sleep().

If you leave a thread sleeping for too long, the allotted time given by the operating system will get wasted.

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

Which out of wait() and sleep() needs to occur within a synchronized block?

A

wait().

wait() can only be called from within a synchronized block, and will wake up when notify() is called.

17
Q

What are the four main stages of a thread’s lifecycle?

A

New -> Runnable -> Running -> Dead

18
Q

What is meant by a “dead” thread?

A

A dead thread has finished its execution, and has had its memory reallocated.

19
Q

What are the three stages that a thread may reach during the runnable and running phases of a thread’s lifecycle?

A

Waiting, Sleeping or Blocked.

TRIVIA: “Sleeping” is sometimes called “Time Waiting”, to signify that it is wasting the process’ time-slice.

20
Q

What is meant by a “blocked” thread?

A

This may mean that the thread has halted execution due to requiring data from a synchronized, locked method.

21
Q

What is meant by a thread “lock”?

A

A lock is a tool to control access to a shared resource by multiple threads.

These locks may be defined explicitly, but most of the time they may be automatically defined by synchronized blocks.