Concepts Flashcards
Some concept that is relevant to concurrent programming.
What are the four necessary conditions for deadlock?
Mutual exclusion, hold and wait, no resource preemption and circular wait.
What is mutual exclusion?
Mutual exclusion is a concept where only one thread can access a resource at a time.
What is hold and wait?
It is a situation where a thread can reserve a resource and then wait for another to be available.
What is “no resource preemption”?
It is the concept where a thread cannot be forced to release an held resource.
What is circular wait?
It is the concept where resources can be accessed by threads so that it creates a circular dependency.
What is a deadlock?
A deadlock is a situation that occurs when multiple threads are waiting for each other do release a resource.
What is a livelock?
A livelock is a situation where we have circular resource dependencies between threads and end up not being able to proceed. A key characteristic that differentiates a livelock from a deadlock is that the threads are constantly changing resource with one another.
What is starvation?
Starvation is a situation that occurs when a thread needs a resource to proceed but it is more frequently requested by other threads. Thus the thread that requests the resource less frequently will have to wait a long time to access the resource.
What is race condition?
A race condition occurs when multiple threads try to access and modify shared data at the same time. Due to context switches (change to another thread) the data is dependent on the thread scheduling and thus which thread access the data at what time.
What are four ways you can ensure mutual exclusion?
Stack confinement, instance confinement, thread confinement and immutability.
What is stack confinement?
Stack confinement is a way of ensuring thread-safety by ensuring the object only can be reached through a local variable. Since local variables are intrinsically confined to single thread the object is too. This will keep the object thread-safe.
What is instance confinement?
Instance confinement is a way of ensuring thread-safety by properly using locks to ensure only one threads modifies or accesses the resource at a time. An example of this is a monitor i Java.
What is thread confinement?
Thread confinement is a way of ensuring thread safety by limiting a resource to a single thread. An example of this is the Swing EDT (Event Dispatch Thread).
What is immutability?
Immutability is a way of ensuring thread-safety by making it unchanging. Since it cannot be modified it is inherently thread-safe.
What is a monitor in Java?
A monitor is a class that uses the keyword synchronized. Methods that are synchronized can only be accessed by one thread at a time.