java线程的生产者和消费者的经典案例

java线程两种实现方式:第一个继承方式--继承thread类,另一个是实现Runnerable接口

今天讨论的是关于实现Runnerable接口的一个经典案例

1.共享资源:实现了三种版本的共享资源。

//共享资源
public class PeopleShareResource {
   private String name;
   private Date date;
// //第一版  出现重复消费和重复生产
// //1存入
// public void setResource(String name,Date date) {
//    this.name=name;
//    try {
//       Thread.sleep(10);
//    } catch (InterruptedException e) {
//       e.printStackTrace();
//    }
//    this.date=date;
// }
// //1取出
// public void getResource() {
//    try {
//       Thread.sleep(10);
//    } catch (InterruptedException e) {
//       e.printStackTrace();
//    }
//    System.out.println("姓名:"+this.name+"---"+"日期:"+this.date);
// }

   
   
// // 第二版 解决出现重复消费和重复生产,加入synchronized和判断当前生产者与消费者的状态
//
// // 表示检查当前共享资源状态是否为空
// private boolean isEmpty = true;
//
// // 2存入
// public synchronized void setResource(String name, Date date) {
//
//    try {
//       // 如果为false说明不空,
//       while (!isEmpty) {
//          this.wait();// 不空时,生产者停止生产,进入等待
//       }
//       // 如果为true,为空时,进行生产
//       this.name = name;
//       Thread.sleep(10);
//       this.date = date;
//       // 生产完毕,设置状态为false,不空的状态
//       isEmpty = false;
//       //唤醒消费者
//       this.notifyAll();
//    } catch (Exception e) {
//       e.getMessage();
//    }
// }
//
// // 2取出
// public synchronized void getResource() {
//    try {
//       // 状态为空,
//       while (isEmpty) {
//          this.wait();// 为空,消费者等待,生产者来生产
//       }
//       //不空时候,消费者进行消费
//       System.out.println("姓名:"+this.name+"---"+"日期:"+this.date);
//       //消费完毕
//       isEmpty = true;
//       //唤醒生产者
//       this.notifyAll();
//    } catch (InterruptedException e) {
//       e.printStackTrace();
//    }
// }
   
   
   
   
   // 第三版 解决出现重复消费和重复生产,使用Lock和Condition接口

      // 表示检查当前共享资源状态是否为空
      private boolean isEmpty = true;
      
      private final Lock lock = new ReentrantLock();
      Condition condition = lock.newCondition();

      // 3存入
      public void setResource(String name, Date date) {
         //获取锁
         lock.lock();
         try {
            // 如果为false说明不空,
            while (!isEmpty) {
               condition.await();// 生产者等待,
            }
            // 为空,进行生产
            this.name = name;
            Thread.sleep(10);
            this.date = date;
            // 生产完毕,设置状态为false,不空的状态
            isEmpty = false;
            //唤醒消费者
            condition.signalAll();
         } catch (Exception e) {
            e.getMessage();
         }finally {
            lock.unlock();
         }
      }

      // 3取出
      public void getResource() {
         //获取锁
         lock.lock();
         try {
            // 状态为空,
            while (isEmpty) {
               condition.await();// 等待生产者来生产
            }
            //不空进行消费
            System.out.println("姓名:"+this.name+"---"+"日期:"+this.date);
            //消费完毕
            isEmpty = true;
            //唤醒生产者
            condition.signalAll();
         } catch (InterruptedException e) {
            e.printStackTrace();
         }finally {
            lock.unlock();
      }
   }

2.生产者

public class ThreadProducter  implements Runnable{
   private PeopleShareResource resource;
   
   public ThreadProducter(PeopleShareResource resource) {
      this.resource=resource;
   }
   
   public void run() {
      for (int i = 0; i < 5000; i++) {
         if(i%2==0) {
            resource.setResource("王强", new Date());
         }else{
            resource.setResource("高颖", new Date());
         }
      }
   }}

3.消费者

public class ThreadCoustomer implements Runnable{
   private PeopleShareResource resource;
   
   public ThreadCoustomer(PeopleShareResource resource) {
      this.resource=resource;
   }
   
   public void run() {
      for (int i = 0; i < 5000; i++) {
         resource.getResource();
      }
   }}

4.启动线程

public class ThreadArea{

   public static void main(String[] args) {
      //共享资源
      PeopleShareResource resource=new PeopleShareResource();
      //生产者
      ThreadCoustomer coustomer=new ThreadCoustomer(resource);
      //消费者
      ThreadProducter producter=new ThreadProducter(resource);
      
      // public Thread(Runnable target)
      //Runnable target-->coustomer/producter
      new Thread(coustomer).start();
      new Thread(producter).start();
   }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值