*同步一般都是外层是while循环,里面是同步代码块,再里面是循环
*不安全与死锁都因为在判断中出错
死锁举例:
package com.qianfeng.demo02;
class DeadLockThread implements Runnable {
 private static String milk = "牛奶";
 private static String bread = "面包";
 boolean flag = true;
 public void run() {
  if (flag) {
   synchronized (bread) {
    System.out.println(Thread.currentThread().getName() + "\t已经拥有了"
      + bread + ",还要:" + milk);
    synchronized (milk) {
     System.out.println(Thread.currentThread().getName() + "既有"
       + bread + ",又有:" + milk);
    }
   }
  } else {
   synchronized (milk) {
    System.out.println(Thread.currentThread().getName() + "\t已经拥有了"
      + milk + ",还要:" + bread);
    synchronized (bread) {
     System.out.println(Thread.currentThread().getName() + "既有"
       + milk + ",又有:" + bread);
    }
   }
  }
 }
}
public class Demo_DeadLock {
 public static void main(String[] args) {
  DeadLockThread dt1 = new DeadLockThread();
  DeadLockThread dt2 = new DeadLockThread();
  dt1.flag = true;
  dt2.flag = false;
  Thread t1 = new Thread(dt1, "张三");
  Thread t2 = new Thread(dt2, "李四");
  t1.start();
  t2.start();
 }
}
生产者消费者模式
*建两个同步方法,一个消费,一个生产,用while循环判断条件,在里面加上wait和notify
package com.qianfeng.demo03;
class Egg {
 private int id;
 public Egg(int id) {
  this.id = id;
 }
 public String toString() {
  return "鸡蛋:" + id;
 }
}
class Basket {
 private Egg[] eggs = new Egg[8];// 装鸡蛋的数组
 private int index = 0;// 控制数组的下标
 public synchronized void addEgg(Egg egg) {
  while (index == 8) {
   try {
    System.out.println(Thread.currentThread().getName()
      + "\t容器满了,母鸡处于等待。。");
    wait();// 生产者处于等待,释放锁资源。。。
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  eggs[index] = egg;// 存入鸡蛋//index:0-7
  System.out.println("\t\t生产者生产鸡蛋:" + egg);
  try {
   Thread.sleep((int) (Math.random() * 1000));
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  index++;// 下标向上走一个//index:1-8
  notify();// 唤醒吃货,存在空唤醒
 }
 public synchronized void getEgg() {
  while (index == 0) {
   try {
    System.out.println(Thread.currentThread().getName()
      + "\t容器空了,吃货等待。。");
    wait();// 消费者线程处于等待。。放弃cpu的资源,同时也释放了锁
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  index--;// 下标向下减1,取鸡蛋
  try {
   Thread.sleep((int) (Math.random() * 1000));
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  Egg egg = eggs[index];
  System.out.println("消费者吃鸡蛋:" + egg);
  notify();// 唤醒母鸡,空唤醒
 }
}
class Producer implements Runnable {
 private int id = 1;
 private Basket basket;
 public Producer(Basket basket) {
  this.basket = basket;
 }
 public void run() {
  while (true) {
   Egg egg = new Egg(id++);
   basket.addEgg(egg);
  }
 }
}
class Customer implements Runnable {
 private Basket basket;
 public Customer(Basket basket) {
  this.basket = basket;
 }
 public void run() {
  // 循环消费鸡蛋:
  while (true) {
   basket.getEgg();
  }
 }
}
public class TestProAndCus {
 public static void main(String[] args) {
  Basket basket = new Basket();
  Producer p1 = new Producer(basket);
  Customer c1 = new Customer(basket);
  Thread t1 = new Thread(p1, "母鸡");
  Thread t2 = new Thread(c1, "吃货");
  t1.start();
  t2.start();
 }
}
例子二
package com.qianfeng.homework;
import java.util.ArrayList;
import java.util.List;
class JianEgg {
 private int id;
 static boolean flag = true;//
 public JianEgg(int id) {
  super();
  this.id = id;
 }
 public String toString() {
  return "煎鸡蛋" + id;
 }
}
class Procuder1 implements Runnable {
 private List<JianEgg> plant;
 private int id = 1;
 public Procuder1(List<JianEgg> plant) {
  super();
  this.plant = plant;
 }
 public void run() {
  while (true) {
   synchronized (plant) {
    JianEgg je = new JianEgg(id++);
    while (!JianEgg.flag) {// F:容器是满了,应该消费
     try {
      System.out.println(Thread.currentThread().getName()
        + "\t等待。。");
      plant.wait();
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
    System.out.println("生产者生产鸡蛋:" + je);
    plant.add(je);
    plant.notify();
    JianEgg.flag = false;// 生产后容器满了,改为false,下次就消费
   }
  }
 }
}
class Customer1 implements Runnable {
 private List<JianEgg> plant;
 public Customer1(List<JianEgg> plant) {
  super();
  this.plant = plant;
 }
 public void run() {
  while (true) {
   synchronized (plant) {
    while (JianEgg.flag) {// T:容器是空的,应该生产
     try {
      System.out.println("\t"
        + Thread.currentThread().getName() + "\t等待。。");
      plant.wait();
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
    JianEgg je = plant.get(0);
    plant.remove(0);
    System.out.println("\t\t消费者吃鸡蛋:" + je);
    plant.notify();
    JianEgg.flag = true;// 吃完,容器就空了,改为T,下次该生产了
   }
  }
 }
}
public class HomeWork03 {
 public static void main(String[] args) {
  List<JianEgg> list = new ArrayList<>();
  Procuder1 p1 = new Procuder1(list);
  Customer1 c1 = new Customer1(list);
  Thread t1 = new Thread(p1, "老公");
  Thread t2 = new Thread(c1, "媳妇");
  t1.start();
  t2.start();
 }
}