Concurrency

Starvation

Other than a Deadlock, an application thread can also experience starvation, where it never gets CPU time or access to shared resources because other “greedy” threads hog the resources.

A situation in which a runnable process is overlooked indefinitely by the scheduler; although it is able to proceed, it is never chosen.

How to avoid starvation?

The best way to avoid starvation is to use a lock such as ReentrantLock or a Mutex. This introduces a “fair” lock which favours granting access to the thread that has been waiting longest. If you wanted to have multiple threads run at once while preventing starvation, you can use a Semaphore.