Wk2L2 - Data Races Flashcards

1
Q

What is a Data Race in multithreading?

A

A data race occurs when two threads access the same memory location concurrently, and at least one thread is writing to it without proper synchronization, leading to unpredictable results.

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

What is the main cause of Data Races?

A

Data races happen when multiple threads read and write shared memory without synchronization, causing inconsistent or incorrect values.

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

What is a Critical Section in multithreading?

A

A critical section is the part of the code that accesses shared memory or resources. If not protected, it can lead to data races.

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

How does Context Switching relate to threads?

A

Context switching refers to the process where the operating system switches between different threads or processes, saving and loading their state. This occurs in both preemptive and non-preemptive scheduling.

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

What is Preemptive Scheduling in threading?

A

Preemptive scheduling is when the thread manager (like the OS or JVM) can interrupt a running thread to give control to another thread. This is typically triggered by a timer or when a thread is blocked.

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

What is the difference between Preemptive Round Robin and Fixed-Priority Scheduling?

A

Preemptive Round Robin: Each thread is given equal time slices to execute, regardless of priority.

Fixed-Priority Scheduling: Threads with higher priority get more CPU time or are executed before lower-priority threads, which can lead to starvation.

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

How is the Thread Lifecycle managed in Java?

A

The thread lifecycle in Java includes several states: new, runnable, running, waiting, blocked, and terminated. The getState() method can be used to check a thread’s state.

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

How are Data Races similar to Cache Coherency problems?

A

Both involve multiple “writers” updating the same data, but cache coherency deals with inconsistent cached copies of data across multiple cores, while data races occur when threads access shared memory without proper synchronization.

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

How can Data Races be prevented?

A

Data races can be avoided by using synchronization techniques such as locks, atomic variables, or the synchronized keyword in Java to protect critical sections.

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