Synchronization;Barriers;Semaphores Flashcards
What do we do if we have a memory location which is neither thread-local nor immutable?
We need to make sure we synchronize the resource properly.
What is thread local?
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
What is immutable? And how do we make a resource immutable?
Do not write to the memory location. You can enforcethis by declaring such variable final
Remember there are 3 ways to make concurreny happen, at least one of them needs to be met. What are these 3?
Synchronized
Immutable
Thread local
What does a thread do when encountering a synchronized keyword?
First it attempts to acquire the lock to the specified object. Until it obtains the lock it will block
Are Java lock reentrant and what does this mean?
Java locks are reentrent. A lock is reentren if it can be acquired multiple times by the same thread
How do changes made inside a synchronized block appear to other thread?
They appear to take place instanttaneously
What problem do long critical sections protected by locks do to the program? Thinking of Amdahls Law
We reduce the possible speedup of the program, because critical sections protected by locks arent parallizable. Remember sequential work is a bottle neck.
Where can conditions be implemented?
Critical sections protected by locks and the thread must hold the lock to react to the condition.
How do we implement conditions on locks in Java?
List the methods
- wait()
- notify()
- notifyAll()
What does wait() do?
Thread is moved into an inactive state and is inserted into the waiting queue. The thread releases the lock on which wait() was called.
what does notify() do?
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
What does notifyAll() do?
- All threads are removed from the waiting queue and moved into an active state.
- All these threads will then attempt to reacquire the lock.
Why do we use while loops insted of if statements when using conditions in locked sections?
- the operating system might randomly wake up a waiting thread
- another thread might interrupt the waiting thread this leads to an InterruptException
What are the methods in java.util.concurrent.locks library called? and what equals wait(), notify() and notifyAll()
The methods are
await() is equal to wait()
signal() is equal to notify()
signalAll() is equal to notifyAll()