码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程问题中死锁的一个实现

时间:2015-05-19 22:42:30      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:deadlock

1.直接上代码:

class LockDemo{
	public static final Object A_LOCK=new Object();
	public static final Object B_LOCK=new Object();
}
public class ThreadLockDemo implements Runnable{
	public boolean flag;
	public ThreadLockDemo(boolean flag)
	{
		this.flag=flag;
	}
	public void run()
	{
		if(flag)
		{
			synchronized (LockDemo.A_LOCK) {
				System.out.println("if  A_LOCK");
				synchronized (LockDemo.B_LOCK) {
					System.out.println("B_LOCK");
				}
				
			}
		}
		else
		{
			synchronized (LockDemo.B_LOCK) {
				System.out.println("else B_LOCK");
				synchronized (LockDemo.A_LOCK) {
					System.out.println("A_LOCK");
				}
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ThreadLockDemo td1=new ThreadLockDemo(false);
		ThreadLockDemo td2=new ThreadLockDemo(true);
		Thread t1=new Thread(td1);
		Thread t2=new Thread(td2);
		t1.start();
		t2.start();
	}

}

2.有图有真相

技术分享

java多线程问题中死锁的一个实现

标签:deadlock

原文地址:http://blog.csdn.net/mnmlist/article/details/45850183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!