jdk线程的死锁

两个线程相互等着对方释放同步监听器;等着要对方的结果后才能继续执行就会发生死锁。

男对女说:你先嫁给我,我再给你买房子;女对男说:你先给我买房子,我再嫁给你。

多个线程同时锁住同一个监听对象

在开发中要避免死锁。

 

死锁的例子:

public class DeadLock {

    public static void main(String[] args) {

        Resource r1 = new Resource();

        Resource r2 = new Resource();

        

        // 每个线程都拥有r1,r2两个对象

        Thread myTh1 = new MyThread1(r1, r2);

        Thread myTh2 = new MyThread2(r1, r2);

        

        myTh1.start();

        myTh2.start();

    }

}



class Resource {

    private int i;

}



class MyThread1 extends Thread {

    private Resource r1, r2;



    public MyThread1(Resource r1, Resource r2) {

        this.r1 = r1;

        this.r2 = r2;

    }



    @Override

    public void run() {

        while (true) {

            // 先获得r1的锁,再获得r2的锁

            synchronized (r1) {

                System.out.println("1号线程获取了r1的锁");

                synchronized (r2) {

                    System.out.println("1号线程获取了r2的锁");

                }

            }

        }

    }

}



class MyThread2 extends Thread {

    private Resource r1, r2;



    public MyThread2(Resource r1, Resource r2) {

        this.r1 = r1;

        this.r2 = r2;

    }



    @Override

    public void run() {

        while (true) {

            // 先获得r2的锁,再获得r1的锁

            synchronized (r2) {

                System.out.println("2号线程获取了r2的锁");

                synchronized (r1) {

                    System.out.println("2号线程获取了r1的锁");

                }

            }

        }

    }

}

 

你可能感兴趣的:(jdk)