线程死锁

什么是死锁

  • 多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不能正常运行

死锁产生的原因

  • 要产生死锁,必须同时满足以下四个条件
  • 互斥条件:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不能正常运行
  • 不剥夺条件:进程所获得的资源在未使用完毕之前,不能被其他进程强行夺走,即只能由获得该资源的线程自己来释放(只能是主动释放).
  • 请求和保持条件:线程已经保持了至少一个资源,但又提出了新的资源请求,而该资源已被其他线程占有,此时请求线程被阻塞,但对自己已获得的资源保持不放.
  • 循环等待条件:存在一种线程资源的循环等待链,链中每一个线程已获得的资源同时被链中下一个线程所请求。
  • 哲学家吃饭问题
package com.fs.sync;

public class Demo03DeadLockTest implements Runnable{
	public int num = 1;
	private static Object o1 = new Object();
	private static Object o2 = new Object();
	
	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println("num = " + num);
		if (num == 1) {
			synchronized (o1) {
				try {
					System.out.println(name + "锁住o1并执行了" + "num = " + num);
					Thread.sleep(1000);
					System.out.println(name + " o1执行完了");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				synchronized (o2) {
					try {
						System.out.println(name + "锁住o2并执行了" + "num = " + num);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		}
		if (num == 0) {
			synchronized (o2) {
				try {
					System.out.println(name + "锁住o2并执行了" + "num = " + num);
					Thread.sleep(1000);
					System.out.println(name + " o2执行完了");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				synchronized (o1) {
					try {
						System.out.println(name + "锁住o1并执行了" + "num = " + num);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		}
	}
	
	public static void main(String[] args) {
		Demo03DeadLockTest dl = new Demo03DeadLockTest();
		Demo03DeadLockTest dl2 = new Demo03DeadLockTest();
		dl.num = 1;
		dl2.num = 0;
		Thread thread = new Thread(dl, "t1");
		Thread thread2 = new Thread(dl2,"t2");
		thread.start();
		thread2.start();
	}
}

自己总结的死锁代码:

public class ThreadBlock {

    public static void main(String[] args) {
        String str1 = new String("资源1");
        String str2 = new String("资源2");

        new Thread(new Lock(str1, str2), "线程1").start();
        new Thread(new Lock(str2, str1), "线程2").start();
    }
}

class Lock implements Runnable {

    private String str1;
    private String str2;

    public Lock(String str1, String str2) {
        super();
        this.str1 = str1;
        this.str2 = str2;
    }

    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + "运行");
            synchronized (str1) {
                System.out.println(Thread.currentThread().getName() + "锁住"
                        + str1);
                /*
                *这个地方线程1打印的是:线程1锁住了资源1
                *这个地方线程2打印的是:线程2锁住了资源2
                */
                	Thread.sleep(1000);
                synchronized (str2) {
                    System.out.println(Thread.currentThread().getName()
                            + "锁住" + str2);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值