4 thread deadlock

1 definition of deadlock

The so-called deadlock refers to the phenomenon of two or more threads in the implementation process, a result of competition for resources caused by waiting for each other, in the absence of external force, they will not be able to promote it. Deadlock is a multi-threaded programming the negative effect in the programming needed to prevent deadlocks.

 

2 necessary conditions for deadlock

(1) mutually exclusive conditions: process requires resources (such as printers) is assigned exclusive control, i.e. a period of time a resource is occupied for only one process. At this time if other processes the resource request, the request process can only wait.

 

(2) does not deprive condition: resources obtained in the process before completion is not used, can not be forcibly taken away by other processes, which can only be obtained by the process of the resources themselves to release (only active release).

 

(3) requests and keeping conditions: the process has been maintained for at least one resource, but proposed a new resource request, the resource has been occupied by another process, this time requesting process is blocked, but the resources that they have acquired remain put.

 

(4) wait condition loop: there is a waiting loop chain process resource, each resource chain acquired simultaneously process required for the next process in the chain. I.e., there is a process in a waiting state set {Pl, P2, ..., pn}, where Pi is waiting for a resource P (i + 1) occupancy (i = 0, 1, ..., n-1), Pn waiting for resources occupied P0, as shown in FIG.

 

3 Examples of deadlock

(1) a synchronization code blocks Deadlock

public class DeadLockTest {

    public static void main(String[] args) {
        Customer customer = new Customer();
        Hotel hotel = new Hotel();

        new Thread(new Dining(customer, hotel)).start();
        new Thread(new Pay(customer, hotel)).start();
    }
}

class Dining implements Runnable {

    private Customer customer;
    private Hotel hotel;

    public Dining(Customer customer, Hotel hotel) {
        this.customer = customer;
        this.hotel = hotel;
    }

    @Override
    public void run() {
        synchronized (customer) {
            customer.dining();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (hotel) {
                hotel.service();
            }
        }
    }
}

class Pay implements Runnable {

    private Customer customer;
    private Hotel hotel;

    public Pay(Customer customer, Hotel hotel) {
        this.customer = customer;
        this.hotel = hotel;
    }

    @Override
    public void run() {
        synchronized (hotel) {
            hotel.bill();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (customer) {
                customer.pay();
            }
        }
    }
}
View Code

 

class the Customer { 

    public  void Pay () { 
        System.out.println ( "pay customer" ); 
    } 

    public  void Dining () { 
        System.out.println ( "customer at" ); 
    } 
} 

class Hotel { 

    public  void -Service () { 
        System.out.println ( "serving" ); 
    } 

    public  void Bill () { 
        System.out.println ( "please pay" ); 
    } 
}
View Code

 

(2) Synchronization Deadlock

public class DeadLockDemo {

    public static void main(String[] args) {
        new DeadThread();
    }
}

class Customers {

    public synchronized void dining(Waiter w) {
        System.out.println("先吃饭再买单");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        w.doService();
    }

    public synchronized void pay() {
        System.out.println ( "agree to pay for it" ); 
    } 
} 

class Waiter {
     public  the synchronized  void Bill (the Customers c) { 
        System.out.println ( "pay first and then eat" );
         the try { 
            Thread.sleep ( 2000 ); 
        } the catch (InterruptedException E) { 
            e.printStackTrace (); 
        } 
        c.pay (); 
    } 

    public  the synchronized  void the doService () { 
        System.out.println ( "consent to eat it" ); 
    } 
} 

class DeadThread implements Runnable {
    Customers customer = new Customers();
    Waiter waiter = new Waiter();

    public DeadThread() {
        new Thread(this).start();
        waiter.bill(customer);
    }

    @Override
    public void run() {
        customer.dining(waiter);
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/Latiny/p/10659609.html