产生死锁的条件:
1.有至少一个资源不能共享
2.至少有一个任务必须持有一个资源并且等待获取另一个被别的任务持有的资源
3.资源不能任务抢占
4.必须有循环等待
package com.thread; /** * 死锁简单示例 * 死锁的原因是由于 两个线程相互等待 对方已被锁定的资源 * @author YangBaoBao * */ public class DeadLock { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Object obj=new Object(); Object obj1=new Object(); DeadLockThread diedLock=new DeadLockThread(obj,obj1); DeadLockThread1 diedLock1=new DeadLockThread1(obj,obj1); diedLock.start(); diedLock1.start(); } } class DeadLockThread extends Thread { private Object obj; private Object obj1; public DeadLockThread(Object obj,Object obj1) { this.obj=obj; this.obj1=obj1; } @Override public void run() { synchronized (obj) { try { Thread.sleep(100); synchronized (obj1) { obj1.getClass(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class DeadLockThread1 extends Thread { private Object obj; private Object obj1; public DeadLockThread1(Object obj,Object obj1) { this.obj=obj; this.obj1=obj1; } @Override public void run() { synchronized (obj1) { try { Thread.sleep(100); synchronized (obj) { obj.getClass(); } } catch (InterruptedException e) { e.printStackTrace(); } } } }