chapter 4 Flashcards

1
Q

What does the Happens Before Guarantee for Reads of volatile Variables state?

A

A read of a volatile variable will happen before any subsequent reads of volatile and non-volatile variables

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

What can happen if two or more threads are sharing an object without proper synchronization or volatile declarations?

A

Updates to the shared object made by one thread may not be visible to other threads.

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

What happens if a thread running on CPU one changes the shared object in its CPU cache before flushing it back to main memory?

A

The changed version of the shared object is not visible to threads running on other CPUs.

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

Give an example of a race condition (train ticket booking)

A

If two passengers try to book the only available ticket at the same time without synchronization, both might end up booking tickets, causing a problem.

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

What are special hardware instructions that allow us to modify the content of a word or swap the contents of two words atomically?

A

Test-and-Set instruction and Compare-and-Swap instruction are special hardware instructions.

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

How can you solve the problem of race condition with volatile variables?

A

You can use a Java synchronized block to guarantee only one thread can enter a given critical section of the code at any given time.

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

Imagine thread A reads the variable count into its CPU cache, while thread B does the same into a different CPU cache. What happens if both threads increment the count?

A

The variable count will be incremented twice, once in each CPU cache.

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

Do volatile variables guarantee solving the synchronization problem for race condition?

A

No, volatile variables do not guarantee solving the synchronization problem for race condition.

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

What is the purpose of the AtomicBoolean variable in the counter example?

A

The AtomicBoolean variable is used as a lock to provide synchronization.

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

What is the purpose of a memory barrier instruction?

A

To ensure that all loads and stores are completed before any subsequent load or store operations are performed.

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

What can a memory barrier instruction prevent?

A

Reordered instructions and ensure store operations are completed in memory and visible to other processors before future load or store operations are performed.

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

What can happen in multithreading applications without proper synchronization?

A

Reorder instruction problems, where instructions can be reordered leading to unexpected behavior.

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

What do computer architectures provide to ensure memory modifications are visible to threads running on other processors?

A

Memory barriers or memory fences.

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

Why can’t kernel developers make assumptions about memory modifications on a shared-memory multiprocessor?

A

Memory models vary by processor type.

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

What is a weakly ordered memory model?

A

Where a memory modification of one processor may not be immediately visible to all other processors.

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

What happens if synchronization is violated?

A

Multiple threads may access shared resources simultaneously, leading to unpredictable results.

17
Q

How can static synchronization be achieved in Java?

A

By applying the synchronized keyword to static methods or using a synchronized block with a static object.

18
Q

When does a thread acquire the monitor and continue executing the monitor region?

A

If no other thread is waiting in the entry set and no other thread currently owns the monitor.

19
Q

What happens if a thread arrives at the beginning of a monitor region that is already owned by another thread?

A

The newly arrived thread must wait in the entry set.

20
Q

What happens when the current owner of the monitor exits the monitor?

A

The newly arrived thread must compete with any other threads waiting in the entry set.

21
Q

When is cooperation important in Java monitors?

A

When one thread needs some data to be in a particular state and another thread is responsible for getting the data into that state.

22
Q

What should you do if you need to guarantee the exact sequence of thread access to a synchronized block?

A

implement Fairness yourself.

23
Q

How does static synchronization work?

A

It places the synchronized keyword before a static method, ensuring that the lock access is on the class, not on the object or method.

24
Q

What is static synchronization?

A

It is a way to ensure that multiple threads do not simultaneously enter a synchronized block or method with separate locks.

25
Q

How does static synchronization impact instruction reordering?

A

It places restrictions on the reordering of instructions before, inside, and after synchronized blocks, similar to the restrictions imposed by the volatile keyword.

26
Q

What does the synchronized keyword do?

A

It refreshes the values of all variables visible to the thread when it enters a synchronized block and commits changes to variables visible to the thread when it exits the block.

27
Q
A