You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can have threading problems with multi-threaded applications.
E.g. two threads can block each other from accessing code
The Concurrency API helps mitigate these isssues but they can still occur
🟥 7.7.1 Understanding Liveness
Some thread operations can be performed independently, but some need coordination like when we synchronise a method which means the threads wait for threads to finish before moving on.
E.g. the CyclicBarrier enforces a lock on the threads until a specified number of threads are waiting reach the barrier limit
Liveness is the ability for the app to run in a timely manner, with no unexpected delays
There are 3 types of Liveness Problems:
Deadlock
Starvation
Livelock
🟡 Deadlock
Deadlock occurs when two or more threads are blocked forever, waiting for each other.
Suppose we have two foxes:
Foxy - likes to eat, then drink. Can not drink while Tails is drinking
Tails - likes to drink, then eat. Can not eat while Foxy is eating
It takes 100ms for the foxes to run from one environment to the other
As we can see foxy is blocked from drinking, and tails is blocked from eating!
If we removed the synchronized blocks, then both foxy and tail would've been able to both eat and drink.
🟡 Starvation
Starvation happens when a single thread is continually denied access to a resource or lock. It is active but unable to finish task due to other threads taking the resource
🟡 Livelock
Livelock is a special case of starvation. When two threads attempt to resolve a deadlock by continually attempting to acquire resources but are unable to do so, and restart part of the process
🟥 7.7.2 Managing Race Conditions
A Race Condition is a an undesirable result which occurs when two task are completed at the same time, when they should be done sequentially.
Suppose we have a website, and two different users attempt to register with username ZooFan at the same time
This is a race condition with the following outcomes:
Both uses are able to register the username - UNDESIRABLE
Both users are unasble to register - NOT UNDESIREABLE
One user is able to register, while the other recieves an error messager - MOST DESIRABLE