• java 中多线程之间的通讯之生产者和消费者 (多个线程之间的通讯)


    在真实开发 中关于多线程的通讯的问题用到下边的例子是比较多的

    不同的地方时if 和while 的区别 如果只是两个线程之间的通讯,使用if是没有问题的。

    但是在多个线程之间就会有问题

      1 /*
      2  * 这个例子用来解释多个生产者和多个消费者的情况
      3  */
      4 
      5 /*
      6  * 资源库
      7  */
      8 class Resource 
      9 {
     10     private String name;
     11     private int count = 1;
     12     private boolean flag = false;
     13     public synchronized void set (String name){
     14         
     15         while (flag)
     16             try {this.wait();}
     17             catch(Exception e){
     18                 
     19             }
     20             this .name = name + "--" + count++;
     21             System.out.println(Thread.currentThread().getName()+"生产商品"+this.name);
     22             flag = true;
     23             this.notifyAll();
     24             
     25         }
     26     
     27     
     28     public synchronized void out(){
     29         while (!flag)
     30             try {this.wait();}
     31             catch(Exception e){
     32                 
     33             }
     34             System.out.println(Thread.currentThread().getName() + "消费了" + this.name);
     35             flag = false;
     36             this.notifyAll();
     37         }
     38         
     39     }
     40 
     41 /*
     42  * 生产者
     43  */
     44 class Producer implements Runnable
     45 {
     46     private Resource res;
     47     Producer (Resource res){
     48         
     49         this.res = res;
     50     }
     51     public void run(){
     52         
     53         while (true){
     54             
     55             res.set ("牙膏");
     56         }
     57     }
     58     
     59     
     60 }
     61 /*
     62  * 消费者
     63  */
     64 class Consumer implements Runnable
     65 {
     66 
     67     private Resource res;
     68     Consumer(Resource res){
     69         
     70         this.res = res;
     71     }
     72     public void run(){
     73         
     74         while (true){
     75             res.out();
     76         }
     77     }
     78 }
     79 public class producerConsumerDemo {
     80 
     81     public static void main(String[] args) {
     82         // TODO Auto-generated method stub
     83         Resource res = new Resource();
     84         
     85         Producer p1 = new Producer(res);
     86         Producer p2 = new Producer(res);
     87         
     88         Consumer c1 = new Consumer(res);
     89         Consumer c2 = new Consumer(res);
     90         
     91         Thread t1 = new Thread(p1);
     92         Thread t2 = new Thread(p2);
     93         Thread t3 = new Thread(c1);
     94         Thread t4 = new Thread(c2);
     95         
     96         t1.start();
     97         t2.start();
     98         t3.start();
     99         t4.start();
    100 
    101 
    102 
    103     }
    104 
    105 }
  • 相关阅读:
    CentOS8下升级Python3.6到3.9
    web service基础知识
    mysql+centos7+主从复制
    saltstack高效运维
    Docker
    python如何配置virtualenv
    Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
    nginx+uWSGI+django+virtualenv+supervisor发布web服务器
    RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1
    flask-wtforms
  • 原文地址:https://www.cnblogs.com/machao/p/4605714.html
Copyright © 2020-2023  润新知