zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    _ 下划线 Underscores __init__
    Page not found (404) 不被Django的exception中间件捕捉 中间件
    从装修儿童房时的门锁说起
    欧拉定理 费马小定理的推广
    线性运算 非线性运算
    Optimistic concurrency control 死锁 悲观锁 乐观锁 自旋锁
    Avoiding Full Table Scans
    批量的单向的ssh 认证
    批量的单向的ssh 认证
    Corrupted MAC on input at /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux/Net/SSH/Perl/Packet.pm l
  • 原文地址:https://www.cnblogs.com/fuck1/p/5432683.html
Copyright © 2011-2022 走看看