2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Difference between deadlock and starvation in java

what is the difference between deadlock and starvation in java.
userimage

The basic difference between deadlock and starvation in java is:


1)   A long waiting of a Thread which never ends is called deadlock.
2)   A long waiting of a Thread which ends at certain point is called starvation.
3)   A low priority Thread has to wait until completing all high priority Threads.
4)   This long waiting of Thread which ends at certain point is called starvation.

userimage

To make the above answer more simplifying,


Deadlock and starvation are some of the problems that occur due to data races and race conditions that occur during programming as well as implementing hardware. In a deadlock, two threads will infinitely wait for each other without executing while, in a starvation, one thread will roll back and let the other thread to use the resources. A deadlock will cause starvation whereas starvation will help a thread to get out from a deadlock.


A deadlock occurs when two (or more) threads have created a situation where they are all blocking each other. Imagine that threads T1 and T2 need to acquire both resources A and B in order to do their work. If T1 acquires resource A, then T2 acquires resource B, T1 could then be waiting for resource B while T2 was waiting for resource A. In this case, both threads will wait indefinitely for the resource held by the other thread. These threads are said to be deadlocked.


Starvation occurs when a scheduler process (i.e. the operating system) refuses to give a particular thread any quantity of a particular resource (generally CPU). If there are too many high-priority threads, a lower priority thread may be starved. This can have negative impacts, though, particularly when the lower-priority thread has a lock on some resource.


Answer is