简单生产消费模式的代码流程(Java代码)

Student
package july.star.thread12;
 /**
  * Student
  * @author MoXingJian
  * @email 939697374@qq.com
  * @date 2016年11月30日 下午3:39:45
  * @version 1.0
  */
public class Student {
     //定义成员变量
      String name;
      Integer age;
      boolean flag;
     
}



GetThread
package july.star.thread12;
/**
 * GetThread
 *
 * @author MoXingJian
 * @email 939697374@qq.com
 * @date 2016年11月30日 下午3:42:05
 * @version 1.0
 */
public class GetThread implements Runnable {
     private Student s;
     public GetThread(Student s) {
          this.s = s;
     }
     @Override
     public void run() {
          while (true) {
              synchronized (s) {
                   //1、没数据,flag默认为false,进来
                   if (!s.flag) {
                        try {
                              //2  :等待时释放锁
                             s.wait();   //10、回到这里继续执行
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   }
                   //11、输出结果
                   System.out.println(s.name + ":" + s.age);
                   //标记
                   s.flag = false;  //12、标记并唤醒
                   //唤醒
                   s.notify();
                   //13、抢占执行权
              }
          }
     }
}




SetThread
package july.star.thread12;
/**
 * SetThread
 *
 * @author MoXingJian
 * @email 939697374@qq.com
 * @date 2016年11月30日 下午3:40:31
 * @version 1.0
 */
public class SetThread implements Runnable {
     private Student s;
     private int count;
     public SetThread(Student s) {
          this.s = s;
     }
     @Override
     public void run() {
          while (true) {
              synchronized (s) {
                   //3:没数据,为false,不走wait()
                   if (s.flag) {
                        try {
                             s.wait();  //9、等待并释放锁,转到GetThread执行
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   }
              
                   if (count % 2 == 0) {
                        s.name = "一菲";   //4、赋值
                        s.age = 21;
                        count++;
                   } else {
                        s.name = "二姐"; 
                        s.age = 28;
                        count++;
                   }
                   
                   //修改标记
                   s.flag = true;  //6
                   //唤醒
                   s.notify();  //7、唤醒
              }
              //8、
              //抢占CPU执行权,SetThread有或GetThread有权执行
              //假设SetThread又抢到了 
          }
     }
}



TestThread
package july.star.thread12;
 /**
  * 简单生产消费模式
  * @author MoXingJian
  * @email 939697374@qq.com
  * @date 2016年11月30日 下午3:43:19
  * @version 1.0
  */
public class TestThread {
     public static void main(String[] args) {
          //创建对象
          Student s = new Student();
          
          //创建线程
          SetThread st = new SetThread(s);
          GetThread gt = new GetThread(s);
          
          Thread t1 = new Thread(st);
          Thread t2 = new Thread(gt);
          //开启线程
          t1.start();
          t2.start();
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值