A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘潘敏 中级黑马   /  2015-4-9 01:16  /  552 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求生产者消费者问题的代码和解释

2 个回复

正序浏览
我来帮你回复吧!回复 了就休息的
  1. package cn.itcast_07;
  2. //在这个类中封装了设置和获取的方法(生产者和消费者)
  3. public class Student {
  4.         //定义的私有成员变量
  5.         private String name;
  6.         private int age;
  7.         private boolean flag;
  8.         //这个是在成员方法上加锁,其锁为this
  9.         public synchronized void set(String name, int age) {
  10.                 //首先为false
  11.                 if (this.flag) {
  12.                         try {
  13.                                 this.wait();
  14.                         } catch (InterruptedException e) {
  15.                                 e.printStackTrace();
  16.                         }
  17.                 }

  18.                 // 1@设置名字和年龄(先生产出来)
  19.                 this.name = name;
  20.                 this.age = age;

  21.                 // 修改标记,叫醒下一个线程(单)
  22.                 this.flag = true;
  23.                 this.notify();
  24.                 //有可能继续有使用权,到上面就会在if里睡觉(等待着被叫醒)
  25.                 //必须由下面消费了才有可能在生产
  26.         }
  27.         //这里同理
  28.         public synchronized void get() {
  29.                 if (!this.flag) {
  30.                         try {
  31.                                 this.wait();
  32.                         } catch (InterruptedException e) {
  33.                                 e.printStackTrace();
  34.                         }
  35.                 }

  36.                 //2@ 获取数据(消费)
  37.                 System.out.println(this.name + "---" + this.age);

  38.                 // 修改标记
  39.                 this.flag = false;
  40.                 this.notify();
  41.                 //有可能继续有使用权,到上面就会在if里睡觉(等待着被叫醒)
  42.                 //消费过了必须 先上面生产再消费
  43.         }
  44. }
复制代码
  1. package cn.itcast_07;
  2. //一个生产式的多线程
  3. public class SetThread implements Runnable {

  4.         private Student s;
  5.         private int x = 0;

  6.         public SetThread(Student s) {
  7.                 this.s = s;
  8.         }

  9.         @Override
  10.         public void run() {
  11.                 while (true) {
  12.                         if (x % 2 == 0) {
  13.                                 s.set("中南海", 27);
  14.                         } else {
  15.                                 s.set("黄鹤楼", 30);
  16.                         }
  17.                         x++;
  18.                 }
  19.         }
  20. }
复制代码
  1. package cn.itcast_07;
  2. //一个消费式的多线程
  3. public class GetThread implements Runnable {
  4.         private Student s;
  5.         //有参构造
  6.         public GetThread(Student s) {
  7.                 this.s = s;
  8.         }
  9.         //重写run方法
  10.         @Override
  11.         public void run() {
  12.                 while (true) {
  13.                         s.get();
  14.                 }
  15.         }
  16. }
复制代码
  1. package cn.itcast_07;
  2. public class StudentDemo {
  3.         public static void main(String[] args) {
  4.                 //创建资源
  5.                 Student s = new Student();
  6.                
  7.                 //一个生产一个消费的类,传递的是其共同操作的资源,这样就可以在其类中调用资                //源s的2个被封装的方法了
  8.                 SetThread st = new SetThread(s);
  9.                 GetThread gt = new GetThread(s);

  10.                 //线程类
  11.                 Thread t1 = new Thread(st);
  12.                 Thread t2 = new Thread(gt);

  13.                 //启动线程
  14.                 t1.start();
  15.                 t2.start();
  16.         }
  17. }
复制代码
把Student的成员变量给私有的了。
把设置和获取的操作给封装成了功能,并加了同步。
设置或者获取的线程里面只需要调用方法。
我已经用其全部能力说清楚了!不是你不懂就是我不懂,舍其无它!!!
回复 使用道具 举报
你这问题描述太笼统了……最好是把代码发上来。然后指出看不懂的地方
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马