Multi-threading & Concurrency Flashcards

1
Q

What is a thread?

A

A lightweight unit of execution within a process that shares the same memory space and resources

Threads enable concurrent execution within the same process.

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

What is a process?

A

An independent program in execution with its own memory space

Processes are isolated from one another.

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

What is the key difference between threads and processes?

A

Multiple threads can run in the same process and share memory, while processes are isolated from each other

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

Define synchronous execution.

A

Tasks run one after the other, blocking until each is completed

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

Define asynchronous execution.

A

Tasks can run in parallel or independently without waiting for others to complete

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

What is the purpose of the synchronized keyword?

A

Ensure that only one thread can execute a block of code or method at a time

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

What are the two approaches to creating threads in Java?

A
  • Extend Thread
  • Implement Runnable (interface)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is it considered best practice to implement Runnable in Java?

A

Java supports single inheritance, meaning if you use extend, you can only extend Thread

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

What does the sleep() method do?

A

Pauses the execution for a specified time but does not release any locks

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

What does the wait() method do?

A

Releases the lock and waits for another thread to notify using notify() or notifyAll()

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

What are Reentrant Locks?

A

A more flexible locking mechanism than synchronized, allowing more control (e.g. try-locking)

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

Define deadlock.

A

A situation where two or more threads are waiting indefinitely for each other’s resources, causing a standstill

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

What are some strategies to prevent deadlock?

A
  • Lock ordering
  • Try-lock mechanisms
  • Apply a timeout strategy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a race condition?

A

Occurs when two threads share data simultaneously, and the result depends on the thread scheduling

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

What is a solution to prevent race conditions?

A

Use synchronization or atomic variables

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

What does the volatile keyword do?

A

Ensures that changes to a variable are always visible to other threads and prevents caching of variables across threads

17
Q

List the thread lifecycle states.

A
  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed waiting
  • Terminated
18
Q

What does the Runnable interface do?

A

Returns no result and cannot throw checked exceptions

19
Q

What is the main difference between Callable and Runnable?

A

Callable can return a result and throw exceptions, while Runnable cannot

20
Q

What is a New thread state?

A

Thread instance is created but not yet started

21
Q

What is a Runnable state?

A

Thread is ready to run, waiting for CPU time

22
Q

What is the blocked thread state?

A

Waiting for a monitor lock

23
Q

What is the waiting thread state?

A

Waiting indefinitely until notified

24
Q

What is timed waiting thread state?

A

Waiting for a specified period

25
Q

What is terminated thread state?

A

Finished execution