Synchronization;Barriers;Semaphores Flashcards

1
Q

What do we do if we have a memory location which is neither thread-local nor immutable?

A

We need to make sure we synchronize the resource properly.

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

What is thread local?

A

Do not use the memory location in more than 1 thread, by giving each thread its own copy of a resource, provided that the threads dont need to communicate through this resource

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

What is immutable? And how do we make a resource immutable?

A

Do not write to the memory location. You can enforcethis by declaring such variable final

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

Remember there are 3 ways to make concurreny happen, at least one of them needs to be met. What are these 3?

A

Synchronized
Immutable
Thread local

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

What does a thread do when encountering a synchronized keyword?

A

First it attempts to acquire the lock to the specified object. Until it obtains the lock it will block

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

Are Java lock reentrant and what does this mean?

A

Java locks are reentrent. A lock is reentren if it can be acquired multiple times by the same thread

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

How do changes made inside a synchronized block appear to other thread?

A

They appear to take place instanttaneously

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

What problem do long critical sections protected by locks do to the program? Thinking of Amdahls Law

A

We reduce the possible speedup of the program, because critical sections protected by locks arent parallizable. Remember sequential work is a bottle neck.

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

Where can conditions be implemented?

A

Critical sections protected by locks and the thread must hold the lock to react to the condition.

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

How do we implement conditions on locks in Java?

List the methods

A
  • wait()
  • notify()
  • notifyAll()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does wait() do?

A

Thread is moved into an inactive state and is inserted into the waiting queue. The thread releases the lock on which wait() was called.

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

what does notify() do?

A

Some thread is removed from the waiting queue and moved into an ctive state. The thread that was just woken up will then proceed to attempt to re-acquire the lock normally.

We do not have control over which thread is woken up

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

What does notifyAll() do?

A
  • All threads are removed from the waiting queue and moved into an active state.
  • All these threads will then attempt to reacquire the lock.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why do we use while loops insted of if statements when using conditions in locked sections?

A
  • the operating system might randomly wake up a waiting thread
  • another thread might interrupt the waiting thread this leads to an InterruptException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the methods in java.util.concurrent.locks library called? and what equals wait(), notify() and notifyAll()

A

The methods are
await() is equal to wait()
signal() is equal to notify()
signalAll() is equal to notifyAll()

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

what is important to know about the wait() method?

Tipp: spurious wakeups

A

Always enclose a wait() statement in a while loop. Remember the operating system could wake up a thread at any time without calling notify()

17
Q

Only call wait() and notify() under what condition?

A

the corresponding object must hold the lock

18
Q

Code for implementing synchronized keyword.

What variables need to be created and how is the lock acquired?

A
Following is the code for a lock that can be acquired:
private static Object lock = new Object(); 

To acquire the lock just write:
synchronized(lock) {…..}

Example see picture

19
Q

How to implement a condition lock? What is the code? What variables are needed?

A
We need to create a lock like this:
private static Lock l = new ReentantLock();
To create a condition we do this as follows using the lock just created:
private static Condition menQueue = 
l.newCondition();

Acquiring and releasing the lock happens as follows:

l. lock() (acquiring the lock l)
l. unlcok() (releaseing the lock l)

see picture for example

20
Q

How can we let more than one thread enter a critical section?

A

Using Semaphores. A Semaphore S has a capacity of N

21
Q

A Semaphore S with capacity of N has two methods name them.

A

acquire(S): Block until N > 0, then decrements N.

release(S): Increments N

22
Q

What does a Barrier do?

A

It blocks all threads up until a certain threshold of N threads. Once the threshhold has been reached all threads waiting in the barrier are allowed to continue.

23
Q

What are the 2 different types of barriers?

A

reusable and non-reusable

24
Q

What is a reusable barrier?

A

the barrier can reset to its initial state and be reused.

25
Q

what is a non-reusable barrier?

A

it cant be reused

26
Q

Give an implementation of a non-reusable barrier.

How are Semaphores used in the implementation?

A

A new barrier is initiated wit a Semaphore of capacity 0

What is important is that the count for the threshhold of the barrier implements mutual exclusion

27
Q

What is a semaphore using a nightclub as an example?

A

Think of semaphores as bouncers at a nightclub. There are a dedicated number of people that are allowed in the club at once. If the club is full no one is allowed to enter, but as soon as one person leaves another person might enter.

A way to limit the number of consumers for a specific resource.

28
Q

What does the picture say about semaphores.

A

A semaphore makes resources available for a specific number of threads. This number is repsented by an integer. In the picture Thread 1 acquires the semaphore first which has 2 places. After acquiring it the integer is decremented to 1. Now Thread 2 acquires the resource the int value is now 0, so therefore Thread 3 can’t acquire the resource. After realeasing the number increments again and Thread 3 could enter.

29
Q

Give an implementation of a semaphore. What is important to remember?

A

Important all operations are atomic.

S := the value of the semaphore ( the number of bikes available to take)

30
Q

Between what barriers do we distinguish?

A

reusable and non-reusable

31
Q

Give an implementation of a non-reusable barrier. In code form, explain the steps. What arguments does it take?

A

We need a threshhold (threads that need to build up until we continue execution.

We need a counter (counts how many threads have reached the barrier and are waiting)

We also need barrier reference to a new Semaphore

Each thread that arrive increments the counter and attempts to acquire the barrier semaphore, thereby going into a blocked state. Once all threads have arrived count == threshhold, the barrier semaphore is set to 1, allowing one thread to pass. Whenever a thread passes the barrier it immediately releases it again so that the next thread may proceed with its execution.

This method is called turnstile