/** * 死锁举例 * @author lenovo * */ public class DeadlockTest { private static Object obj1 = new Object(); private static Object obj2 = new Object(); public static void main(String[] args) { new DeadlockTest().deadLock(); } private void deadLock(){ new Thread(new Runnable(){ @Override public void run() { synchronized(obj1){ try { Thread.sleep(5000); System.out.println("obj1 locking"); } catch (InterruptedException e) {} synchronized(obj2){ System.out.println("bbb"); } } } }).start(); new Thread(new Runnable(){ @Override public void run() { synchronized(obj2){ try { Thread.sleep(5000); System.out.println("obj2 locking"); } catch (InterruptedException e) {} synchronized(obj1){ System.out.println("aaa"); } } } }).start(); } } //来源:网上抄的
死锁例子2
package com.loong.main; public class ThreadDeadlock { public static void main(String[] args) throws InterruptedException { Object obj1 = new Object(); Object obj2 = new Object(); Object obj3 = new Object(); Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1"); Thread t2 = new Thread(new SyncThread(obj2, obj3), "t2"); Thread t3 = new Thread(new SyncThread(obj3, obj1), "t3"); t1.start(); Thread.sleep(5000); t2.start(); Thread.sleep(5000); t3.start(); } } class SyncThread implements Runnable{ private Object obj1; private Object obj2; public SyncThread(Object o1, Object o2){ this.obj1=o1; this.obj2=o2; } @Override public void run() { String name = Thread.currentThread().getName(); System.out.println(name + " acquiring lock on "+obj1); synchronized (obj1) { System.out.println(name + " acquired lock on "+obj1); work(); System.out.println(name + " acquiring lock on "+obj2); synchronized (obj2) { System.out.println(name + " acquired lock on "+obj2); work(); } System.out.println(name + " released lock on "+obj2); } System.out.println(name + " released lock on "+obj1); System.out.println(name + " finished execution."); } private void work() { try { Thread.sleep(30000); } catch (InterruptedException e) { e.printStackTrace(); } } }
t1 acquiring lock on java.lang.Object@3904dd9e
t1 acquired lock on java.lang.Object@3904dd9e
t2 acquiring lock on java.lang.Object@28147a15
t2 acquired lock on java.lang.Object@28147a15
t3 acquiring lock on java.lang.Object@7605d74a
t3 acquired lock on java.lang.Object@7605d74a
t1 acquiring lock on java.lang.Object@28147a15
t2 acquiring lock on java.lang.Object@7605d74a
t3 acquiring lock on java.lang.Object@3904dd9e // 参考:http://www.importnew.com/9668.html
死锁举例3
package com.loong.main; public class ThreadDeadLock2 { private static Object obj1 = new Object(); private static Object obj2 = new Object(); public static void main(String[] args) { DeadLock2 dl1 =new DeadLock2(obj1,obj2); DeadLock2 dl2 =new DeadLock2(obj1,obj2); dl1.flag = 0; dl2.flag = 1; new Thread(dl1).start(); new Thread(dl2).start(); } } class DeadLock2 implements Runnable{ public int flag =0; private Object ob1; private Object ob2; public DeadLock2(Object o1,Object o2){ this.ob1 = o1; this.ob2 = o2; } @Override public void run() { if(flag==0){ synchronized(ob1){ System.out.println("ob1" + "starting"); try { Thread.sleep(5000); System.out.println("ob1" + "ending"); } catch (InterruptedException e) {} synchronized(ob2){ System.out.println("ob2"); } } } if(flag==1){ synchronized(ob2){ System.out.println("ob2" + "starting"); try { Thread.sleep(5000); System.out.println("ob2" + "ending"); } catch (InterruptedException e) {} synchronized(ob1){ System.out.println("ob1"); } } } } }
// Java线程多线程编程【尚学堂科技马士兵Java基础教程】