Deadlock and Starvation in Java

Deadlock: Deadlock is a situation when two threads are waiting for each other and the waiting is never ends. Here both threads cant completes their tasks.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate Deadlock situation
class DeadlockDemo extends Thread {
    static Thread mainThread;
    public void run()
    {
        System.out.println("Child Thread waiting for"
                          " main thread completion");
        try {
  
            // Child thread waiting for completion
            // of main thread
            mainThread.join();
        }
        catch (InterruptedException e) {
            System.out.println("Child thread execution"
                                           " completes");
        }
    }
    public static void main(String[] args) 
                   throws InterruptedException
    {
        DeadlockDemo.mainThread = Thread.currentThread();
        DeadlockDemo thread = new DeadlockDemo();
  
        thread.start();
        System.out.println("Main thread waiting for "
                            "Child thread completion");
  
        // main thread is waiting for the completion
        // of Child thread
        thread.join();
  
        System.out.println("Main thread execution"
                                      " completes");
    }
}

chevron_right


Output:

Main thread waiting for Child thread completion
Child Thread waiting for main thread completion

Starvation:In Starvation, threads are also waiting for each other. But here waiting time is not infinite after some interval of time, waiting thread always gets the resources whatever is required to execute thread run() method.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate Starvation concept
class StarvationDemo extends Thread {
    static int threadcount = 1;
    public void run()
    {
        System.out.println(threadcount + "st Child"
                            " Thread execution starts");
        System.out.println("Child thread execution completes");
        threadcount++;
    }
    public static void main(String[] args) 
               throws InterruptedException
    {
        System.out.println("Main thread execution starts");
  
        // Thread priorities are set in a way that thread5
        // gets least priority.
        StarvationDemo thread1 = new StarvationDemo();
        thread1.setPriority(10);
        StarvationDemo thread2 = new StarvationDemo();
        thread2.setPriority(9);
        StarvationDemo thread3 = new StarvationDemo();
        thread1.setPriority(8);
        StarvationDemo thread4 = new StarvationDemo();
        thread1.setPriority(7);
        StarvationDemo thread5 = new StarvationDemo();
        thread1.setPriority(6);
  
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
  
        // Here thread5 have to wait beacause of the
        // other thread. But after waiting for some
        // interval, thread5 will get the chance of 
        // execution. It is known as Starvation
        thread5.start();
  
        System.out.println("Main thread execution completes");
    }
}

chevron_right


Output:

Main thread execution starts
1st Child Thread execution starts
Child thread execution completes
2st Child Thread execution starts
Child thread execution completes
3st Child Thread execution starts
Child thread execution completes
4st Child Thread execution starts
Child thread execution completes
Main thread execution completes
5st Child Thread execution starts
Child thread execution completes


My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.