1:两个任务以相反的顺序申请两个锁,死锁就可能出现

2:线程T1获得锁L1,线程T2获得锁L2,然后T1申请获得锁L2,同时T2申请获得锁L1,此时两个线程将要永久阻塞,死锁出现

Java多线程____线程死锁例子_死锁

package com.zs.test;

	public class DeadLock {
		public static String objA = "objA";
		public static String objB = "objB";
	
		public static void main(String[] args) {
			Thread a = new Thread(new RunnableA());
			Thread b = new Thread(new RunnableB());
				a.start();
				b.start();
		}
	}

	class RunnableA implements Runnable {
		@Override
		public void run() {
			while (true) {
				System.out.println("线程A__:锁定objA___休眠3秒___锁定objB");
				synchronized (DeadLock.objA) {
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (DeadLock.objB) {
						System.out.println("___"+DeadLock.objA);
					}
				}
			}
		}
	}

	class RunnableB implements Runnable {
		@Override
		public void run() {
			try {
				while (true) {
					System.out.println("线程B__:锁定objB___休眠3秒___锁定objA");
					synchronized (DeadLock.objB) {
						Thread.sleep(3000);
						synchronized (DeadLock.objA) {
							System.out.println("___"+DeadLock.objB);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}



Java多线程____线程死锁例子_System_02