Java多线程——深入了解“死锁”

  众人皆知的“死锁”到底是什么?

  “死锁”到底是怎样产生的?

   ②占有且等待:某线程拿到一个共享资源X占用后,还等待着被别的线程占用了的共享资源Y(该线程已经占有X,即要等待Y,还不释放X)。
   ④循环等待:线程T1拿到了资源X的锁后,去申请Y的锁;

  “死锁”对程序有什么影响?

  举个栗子,展示会出现“死锁”的情况

package com.xiaoaxiao.test.thread_test.book_test;
		
		/**
		 * Created by xiaoaxiao on 2019/7/17
		 * Description: 死锁
		 */
		
		class DealThread implements Runnable{
		
		    public String username;
		    public Object lock1 = new Object();
		    public Object lock2 = new Object();
		
		    public void setFlag(String username){
		        this.username = username;
		    }
		
		    @Override
		    public void run() {
		        if (username.equals("a")){
		            synchronized (lock1){
		                try {
		                    System.out.println("username = "+username);
		                    Thread.sleep(3000);
		                } catch (InterruptedException e) {
		                    e.printStackTrace();
		                }
		                synchronized (lock2){
		                    System.out.println(" 按lock1->lock2 代码顺序执行");
		                }
		            }
		        }
		        if (username.equals("b")){
		            synchronized (lock2){
		                try {
		                    System.out.println("username = "+username);
		                    Thread.sleep(3000);
		                } catch (InterruptedException e) {
		                    e.printStackTrace();
		                }
		                synchronized (lock1){
		                    System.out.println(" 按lock2->lock1 代码顺序执行");
		                }
		            }
		        }
		    }
		
		
		}
		
		public class DealThreadTest {
		
		    public static void main(String[] args) {
		
		        try {
		            DealThread t1 = new DealThread();
		            t1.setFlag("a");
		            Thread thread1 = new Thread(t1);
		            thread1.start();
		            Thread.sleep(1000);
		            t1.setFlag("b");
		            Thread thread2 = new Thread(t1);
		            thread2.start();
		        } catch (InterruptedException e) {
		            e.printStackTrace();
		        }
		    }
		}

  既然“死锁”对程序不好的影响如此之大,那应该如何避免“死锁”呢?