等待唤醒机制
  /*
  *线程等待唤醒机制
  *等待和唤醒必须是同一把锁
  */
  public class ThreadDemo3{
  private static boolean flags=false;
  public static void main(String[]args){
  class Person{
  public String name;
  private String gender;
  public void set(String name,String gender){
  this.name=name;
  this.gender=gender;
  }
  public void get(){
  System.out.println(this.name+"...."+this.gender);
  }
  }
  final Person p=new Person();
  new Thread(new Runnable(){
  public void run(){
  int x=0;
  while(true){
  synchronized(p){
  if(flags)
  try{
  p.wait();
  }catch(InterruptedException e){
  //TODO Auto-generated catch block
  e.printStackTrace();
  };
  if(x==0){
  p.set("张三","男");
  }else{
  p.set("lili","nv");
  }
  x=(x+1)%2;
  flags=true;
  p.notifyAll();
  }
  }
  }
  }).start();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  synchronized(p){
  if(!flags)
  try{
  p.wait();
  }catch(InterruptedException e){
  //TODO Auto-generated catch block
  e.printStackTrace();
  };
  p.get();
  flags=false;
  p.notifyAll();
  }
  }
  }
  }).start();
  }
  }
  生产消费机制一
  public class ThreadDemo4{
  private static boolean flags=false;
  public static void main(String[]args){
  class Goods{
  private String name;
  private int num;
  public synchronized void produce(String name){
  if(flags)
  try{
  wait();
  }catch(InterruptedException e){
  //TODO Auto-generated catch block
  e.printStackTrace();
  }
  this.name=name+"编号:"+num++;
  System.out.println("生产了...."+this.name);
  flags=true;
  notifyAll();
  }
  public synchronized void consume(){
  if(!flags)
  try{
  wait();
  }catch(InterruptedException e){
  //TODO Auto-generated catch block
  e.printStackTrace();
  }
  System.out.println("消费了******"+name);
  flags=false;
  notifyAll();
  }
  }
  final Goods g=new Goods();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  g.produce("商品");
  }
  }
  }).start();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  g.consume();
  }
  }
  }).start();
  }
  }
  生产消费机制2
  public class ThreadDemo4{
  private static boolean flags=false;
  public static void main(String[]args){
  class Goods{
  private String name;
  private int num;
  public synchronized void produce(String name){
  while(flags)
  try{
  wait();
  }catch(InterruptedException e){
  //TODO Auto-generated catch block
  e.printStackTrace();
  }
  this.name=name+"编号:"+num++;
  System.out.println(Thread.currentThread().getName()+"生产了...."+this.name);
  flags=true;
  notifyAll();
  }
  public synchronized void consume(){
  while(!flags)
  try{
  wait();
  }catch(InterruptedException e){
  //TODO Auto-generated catch block
  e.printStackTrace();
  }
  System.out.println(Thread.currentThread().getName()+"消费了******"+name);
  flags=false;
  notifyAll();
  }
  }
  final Goods g=new Goods();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  g.produce("商品");
  }
  }
  },"生产者一号").start();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  g.produce("商品");
  }
  }
  },"生产者二号").start();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  g.consume();
  }
  }
  },"消费者一号").start();
  new Thread(new Runnable(){
  public void run(){
  while(true){
  g.consume();
  }
  }
  },"消费者二号").start();
  }
  }
  /*
  消费者二号消费了******商品编号:48049
  生产者一号生产了....商品编号:48050
  消费者一号消费了******商品编号:48050
  生产者一号生产了....商品编号:48051
  消费者二号消费了******商品编号:48051
  生产者二号生产了....商品编号:48052
  消费者二号消费了******商品编号:48052
  生产者一号生产了....商品编号:48053
  消费者一号消费了******商品编号:48053
  生产者一号生产了....商品编号:48054
  消费者二号消费了******商品编号:48054
  生产者二号生产了....商品编号:48055
  消费者二号消费了******商品编号:48055
  */