Simple Deadlock Program in Java

Simple java program to create Deadlock.


Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.


Let's consider an example, in the office we have shared Printer and Scanner where Employees has ability to do scanning and printing.

1. John has bunch of documents that it wants to Print first and also want to take a Scan later.
(Print and Scan)

2. Michael has bunch of documents that it wants to Scan first and also want to take a Print later.
(Scan and Print)

Deadlock in java with Realtime example. 


package com.javabypatel.threading;

public class DeadlockProgram {
    public static void main(String[] args) {
        Printer printer = new Printer();
        Scanner scanner = new Scanner();

        //John has bunch of documents that it wants to Print and also want to take a scan later
        new Thread (new PrintAndScan(printer, scanner), "John").start();

        //Michael has bunch of documents that it wants to Scan and also want to take a print later
        new Thread (new ScanAndPrint(printer, scanner), "Michael").start();
    }
}

//Thread which take multiple documents and for each document it does the Printing first and then Scans the document
//So it requires two resource Printer and Scanner.
class PrintAndScan implements Runnable {
    Printer printer;
    Scanner scanner;

    public PrintAndScan (Printer printer, Scanner scanner) {
        this.printer = printer;
        this.scanner = scanner;
    }

    @Override
    public void run() {
        //Do printing and scanning simultaneously.
        synchronized (printer) {
            System.out.println("Thread "+Thread.currentThread().getName() + " acquired printer");
            try {
                //Do printing work
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Scan the documents already printed
            System.out.println("Thread "+Thread.currentThread().getName() + " is waiting for scanner");
            synchronized (scanner) {
                System.out.println("Thread "+Thread.currentThread().getName() + " is scanning");
            }
        }
    }
}

//Thread which take multiple documents and for each document it does the Scanning first and then Prints the document
//So it requires two resource Scanner and Printer.
class ScanAndPrint implements Runnable {
    Printer printer;
    Scanner scanner;

    public ScanAndPrint (Printer printer, Scanner scanner) {
        this.printer = printer;
        this.scanner = scanner;
    }

    @Override
    public void run() {
        //Do scanning and printing simultaneously.
        synchronized (scanner) {
            System.out.println("Thread "+Thread.currentThread().getName() + " acquired scanner");
            try {
                //Do scanning work
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Scan the documents already printed
            System.out.println("Thread "+Thread.currentThread().getName() + " is waiting for printer");
            synchronized (printer) {
                System.out.println("Thread "+Thread.currentThread().getName() + " is printing");
            }
        }
    }
}

//Shared Resources
class Printer {}
class Scanner {}


Output:
Thread John acquired printer
Thread Michael acquired scanner
Thread John is waiting for scanner
Thread Michael is waiting for printer

You may also like to see


Sort Linked list using Merge sort

Bubble Sort

Heap Sort

Selection Sort

Insertion Sort


How ConcurrentHashMap works and ConcurrentHashMap interview questions

How Much Water Can A Bar Graph with different heights can Hold

Interview Questions-Answer Bank

Enjoy !!!! 
If you find any issue in post or face any error while implementing, Please comment.

Post a Comment