线程死锁

多线程同步的时候, 如果同步代码嵌套, 使用相同锁, 就有可能出现死锁

 

代码一

package com.demo;

public class testThreadlock {


    public static String obj1 = "obj1";
    public static String obj2 = "obj2";

    public static void main(String[] args) {
        Thread a = new Thread(new Lock1());
        Thread b = new Thread(new Lock2());


        b.start();
        a.start();
    }
}
/**
 * 当线程0拿到s1执行s1中的打印语句时,如果线程切换到线程1
 * 那么线程1拿到了s2锁对象,此时就造成了线程死锁
 *
 * 线程1想执行s1锁里面的代码执行不了,因为s1在线程0中还没有释放
 * 那么此时线程1就会切换到线程0                    
 * 线程0也不会执行s2锁里面的代码,因为此时s2已经被线程0中的锁拿去了
 * 还没有释放,因此造成了线程的死锁两个都没有释放都卡住了线程就卡住了
 */

class Lock1 implements Runnable {


    public void run() {
        while (true) {
            synchronized (testThreadlock.obj1){
                System.out.println(Thread.currentThread().getName()+"obj1");
                synchronized (testThreadlock.obj2){
                    System.out.println(Thread.currentThread().getName()+"obj2");
                }

            }


        }
    }
}

class Lock2 implements Runnable {


    public void run() {
        while (true) {
            synchronized (testThreadlock.obj2){
                System.out.println(Thread.currentThread().getName()+"obj2");
                synchronized (testThreadlock.obj1){
                    System.out.println(Thread.currentThread().getName()+"obj1");
                }

            }

        }
    }
}


代码二

package com.demo;

public class testlinklist {
    public static  String  s1 = "s1";
    public static  String s2 = "s2";
    public static void main(String[] args) {

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    /**
                     * 当线程0拿到s1执行s1中的打印语句时,如果线程切换到线程1
                     * 那么线程1拿到了s2锁对象,此时就造成了线程死锁
                     *
                     * 线程1想执行s1锁里面的代码执行不了,因为s1在线程0中还没有释放
                     * 那么此时线程1就会切换到线程0                    
                     * 线程0也不会执行s2锁里面的代码,因为此时s2已经被线程0中的锁拿去了
                     * 还没有释放,因此造成了线程的死锁两个都没有释放都卡住了线程就卡住了
                     */
                    synchronized (s1) {

                        System.out.println(this.getName() + "s1");


                        synchronized (s2) {

                            System.out.println(this.getName() + "s1");


                        }
                    }


                }

            }


    }.start();


        new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (s2) {

                        System.out.println(this.getName() + "s1");

                        synchronized (s1) {

                            System.out.println(this.getName() + "s1");


                        }
                    }



                }

            }


        }.start();


}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值