多线程等待唤醒机制

public class Res {
 public String name;
 public String sex;
 public boolean flag=false;
}

public class Inputments implements Runnable {
 
 private Res res;
 public Inputments(Res res){
  this.res=res;
 }
 @Override
 public void run() {
  int x=0;
  int y=0;
  while(y<1000){
   synchronized (res) {
    if(res.flag){
     try {
      res.wait();                                                                      //wait() notify() notifyAll() 都要使用在同步中,因为要对持有监视器(锁)的线程操作
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
    if(x==0){
     res.name="wang"+y;
     res.sex="nan";
    }else{
     res.name="张青"+y;
     res.sex="女";
    }
    res.flag=true;
    res.notify();
   }
   System.out.println(Thread.currentThread().getName()+res.name+","+res.sex);
   x=(x+1)%2;
   y++;
  }
  
 }

}

 

 

public class OutMents implements Runnable {
 private Res res;
 public OutMents(Res res){
  this.res=res;
 }
 @Override
 public void run() {
  int y=0;
  while(y<1000){
   synchronized (res) {
    if(!res.flag){
     try {
      res.wait();
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
    System.out.println(Thread.currentThread().getName()+res.name + "," + res.sex);
    res.flag=false;
    res.notify();
   }
   y++;
  }
  
 }
 
}

 

 Res res=new Res();
  Inputments inputments = new Inputments(res);
  OutMents outMents = new OutMents(res);
  Thread th1=new Thread(inputments);
  Thread th2=new Thread(outMents);
  th1.start();
  th2.start();

 

 

多线程通信优化代码:

//资源

public class Resource {
 private String name;
 private String sex;
 private boolean flag =false;
 public synchronized void getName() {
  if(!flag)
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  System.out.println("姓名:"+name+",性别:"+sex);
  flag=false;
  this.notify();
 }
 public synchronized void  setName(String name,String sex) {
   if(flag){
    try {
     this.wait();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   this.name = name+Thread.currentThread().getName();
   this.sex =sex;
   flag = true;
   this.notify();
 }
}
//输入

public class Input implements Runnable {
 private Resource res;
 public Input(Resource res) {
  this.res = res;
 }
 @Override
 public void run() {
  int i=0;
  while(true){
   if(i==0){
    res.setName("张山", "男");
   }else
    res.setName("luce", "woman");
   i =(i+1) %2;
  }
 }

}

 

//输出

package com.thread;

public class Output implements Runnable {
 private Resource res;
 public Output(Resource res) {
  this.res = res;
 }
 @Override
 public void run() {
  int i=0;
  while(i<100){
   res.getName();
   i++;
  }
 }
 public static void main(String[] args) {
  Resource res = new Resource();
  new Thread(new Input(res)).start();
  new Thread(new Output(res)).start();
 }
}

 

生产者和消费者:

对于多个生产者和消费者。

为什么要定义while判读标记

原因:被唤醒的线程再判断一次标记

为什么要定义notifyAll

因为需要唤醒对方线程

因为用notif容易唤醒本方线程,导致所有的线程都等待。

 

//生产者和消费者:

public class ProductionResource {
 private String name;
 private int count =1;
 private boolean flag =false;
 public synchronized void  setName(String name) {
  while(flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.name = name+(count++);
  System.out.println(Thread.currentThread().getName()+"__生产者__名称:"+this.name);

  flag = true;
  this.notifyAll();
  
}
 public synchronized void getName() {
  while(!flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()+"__消费者___名称:"+name);
  flag=false;
  this.notifyAll();
  
 }
 
}

 

//生产者:

 

public class ProductionResource {
 private String name;
 private int count =1;
 private boolean flag =false;
 public synchronized void  setName(String name) {
  while(flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.name = name+(count++);
  System.out.println(Thread.currentThread().getName()+"__生产者__名称:"+this.name);

  flag = true;
  this.notifyAll();
  
}
 public synchronized void getName() {
  while(!flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()+"__消费者___名称:"+name);
  flag=false;
  this.notifyAll();
  
 }
 
}

//消费者:

 

public class Customer implements Runnable {
 private ProductionResource res;
 public Customer(ProductionResource res) {
  this.res = res;
 }
 @Override
 public void run() {
  while(true){
   res.getName();
  }
 }
 public static void main(String[] args) {
  ProductionResource res = new ProductionResource();
  new Thread(new Production(res)).start();
  new Thread(new Production(res)).start();
  new Thread(new Customer(res)).start();
  new Thread(new Customer(res)).start();
 }
}

 

用1.5新技术解决生产和消费问题:

//资源 用Lock 接口 和condition

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProductionResource2 {
 private String name;
 private int count =1;
 private boolean flag =false;
 private Lock lock =new ReentrantLock();
 private Condition conditionPro = lock.newCondition();
 private Condition conditionCus = lock.newCondition();
 public  void  setName(String name) {
  lock.lock();
  try {
   while(flag){
    conditionPro.await();
   }
   this.name = name+(count++);
   System.out.println(Thread.currentThread().getName()+"__生产者__名称:"+this.name);
   flag = true;
   conditionCus.signal();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }finally{
   lock.unlock();
  }
  
}
 public  void getName() {
  lock.lock();
  try {
   while(!flag){
    conditionCus.await();
   }
   System.out.println(Thread.currentThread().getName()+"__消费者___名称:"+name);
   flag=false;
   conditionPro.signal();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  finally{
   lock.unlock();
  }
 }
 
}

//生产

public class Production implements Runnable {
 private ProductionResource2 res;
 public Production(ProductionResource2 res) {
  this.res = res;
 }
 @Override
 public void run() {
  while(true){
   res.setName("洗衣服");
  }
 }
 
}

//消费

public class Customer implements Runnable {
 private ProductionResource2 res;
 public Customer(ProductionResource2 res) {
  this.res = res;
 }
 @Override
 public void run() {
  while(true){
   res.getName();
  }
 }
 public static void main(String[] args) {
  ProductionResource2 res = new ProductionResource2();
  new Thread(new Production(res)).start();
  new Thread(new Production(res)).start();
  new Thread(new Customer(res)).start();
  new Thread(new Customer(res)).start();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值