• java多线程中的生产者与消费者之等待唤醒机制@Version2.0


     二、生产者消费者模式的学生类成员变量生产与消费demo,

    @Version2.0

      在学生类中添加同步方法:synchronized get()消费者,synchronized set()生产者

      最终版的代码中: 把student的成员变量给私有化了,
      把设置和获取的功能给封装成了功能,并加了同步,
      设置或者获取的线程里面只需要调用方法即可。

    1、等待唤醒:
          Object类中提供了三个方法:
          wait():等待
          notify():唤醒单个线程
          notifyAll():唤醒所有线程
    //==========================

    //首先是重点学生类对象

     1 public class Student {
     2     private String name;
     3     private int age;
     4     private boolean flag;
     5 
     6     // 同步方法
     7     public synchronized void set(String name, int age) {
     8         // 如果有数据就等待
     9         if (this.flag) {
    10             try {
    11                 this.wait();
    12             } catch (InterruptedException e) {
    13                 e.printStackTrace();
    14             }
    15         }
    16 
    17         // 设置数据
    18         this.name = name;
    19         this.age = age;
    20 
    21         // 一旦有了数据修改标记,并唤醒
    22         this.flag = true;
    23         this.notify();
    24     }
    25 
    26     // 同步获取方法
    27     public synchronized void get() {
    28         // 如果没有数据就等待
    29         if (!this.flag) {
    30             try {
    31                 this.wait();
    32             } catch (InterruptedException e) {
    33                 e.printStackTrace();
    34             }
    35         }
    36         // 获取数据
    37         System.out.println(this.name + "--- " + this.age);
    38         //获取到数据后,修改标记为false,最后唤醒。
    39         this.flag = false;
    40         this.notify();
    41     }

    // 生产者的线程,这次超级简单~~~··

     1 public class setThread implements Runnable {
     2     private Student s;
     3     private int x = 0;
     4 
     5     public setThread(Student s) {
     6         this.s = s;
     7     }
     8 
     9     @Override
    10     public void run() {
    11         while (true) {
    12 
    13             if (x % 2 == 0) {
    14                 s.set("java", 30);
    15             } else {
    16                 s.set("Android", 20);
    17             }
    18             x++;
    19         }
    20 
    21     }
    22 
    23 }

    // 消费者的线程,这次超级简单~~~··

     1 public class getThread implements Runnable {
     2     private Student s;
     3 
     4     public getThread(Student s) {
     5         this.s = s;
     6     }
     7 
     8     public void run() {
     9         while (true) {
    10             s.get();
    11         }
    12     }
    13 }

    // 消费者的线程,这次超级简单~~~··

     1 public class Demo {
     2     public static void main(String[] args) {
     3         //共享数据,外界创建,作为参数,通过构造共有
     4         Student s = new Student();
     5         //在构造中使用同一个参数
     6         setThread st = new setThread(s);
     7         getThread gt = new getThread(s);
     8 
     9         Thread t1 = new Thread(st);// 设置数据
    10         Thread t2 = new Thread(gt); // 获取数据
    11 
    12         t2.start();
    13         t1.start();
    14     }
    15 }
  • 相关阅读:
    小米9一直无限重启是怎么办
    发现一个大神做了一个ROS-ROUTEROS的中文手册中文使用说明书
    浅谈CN2 GIA和CN2 GT线路的区别
    本地ROS多线访问同一个服务器的IP,比如阿里云的IP,创建冗余线路
    syslog之三:建立Windows下面的syslog日志服务器
    增值税专用发票“抵扣联”和“发票联”丢失怎么办
    在线播放 4K 内容的需要多少带宽?
    戴尔R640服务器用H740P配置阵列
    搞微服务用阿里开源的 Nacos 真香啊!
    保持ssh不自动断开
  • 原文地址:https://www.cnblogs.com/fuck1/p/5432683.html
Copyright © 2020-2023  润新知