`
d3623301984
  • 浏览: 2608 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

生产者消费者JAVA队列实现

    博客分类:
  • java
阅读更多
Java代码
class SyncQueue...{  
    private int head=0;//队头  
    private int tail=1;//队尾  
    public final int Num=6;//缓冲区大小  
    public char data[]=new char[Num];  
    public  synchronized void inqueue(char c)...{  
        while((tail+1)%Num==head)...{  
            try...{  
                this.wait();  
            }catch(InterruptedException e)...{System.out.println(e);}  
              
        }  
        this.notify();  
          
            data[tail]=c;  
            tail=(tail+1)%Num;  
            System.out.println("produced:" + c );  
          
          
    }  
    public  synchronized char outqueue()...{  
        while((head+1)%Num==tail)...{  
            try...{  
                this.wait();  
            }catch(InterruptedException e)...{System.out.println(e);}  
        }  
            this.notify();  
          
      
            head=(head+1)%Num;  
              
            System.out.println("消费:"+data[head]);  
            return data[head];  
          
      
    }  


class SyncQueue...{
    private int head=0;//队头
    private int tail=1;//队尾
    public final int Num=6;//缓冲区大小
    public char data[]=new char[Num];
    public  synchronized void inqueue(char c)...{
        while((tail+1)%Num==head)...{
            try...{
                this.wait();
            }catch(InterruptedException e)...{System.out.println(e);}
           
        }
        this.notify();
       
            data[tail]=c;
            tail=(tail+1)%Num;
            System.out.println("produced:" + c );
       
       
    }
    public  synchronized char outqueue()...{
        while((head+1)%Num==tail)...{
            try...{
                this.wait();
            }catch(InterruptedException e)...{System.out.println(e);}
        }
            this.notify();
       
   
            head=(head+1)%Num;
           
            System.out.println("消费:"+data[head]);
            return data[head];
       
   
    }
}


Java代码
class Producer implements Runnable...{  
    SyncQueue queue;  
    public Producer(SyncQueue s)...{  
        queue=s;  
    }  
    public void run()...{  
        for(int i=0;i<20;i++)...{  
            char c=(char)(Math.random()*26+'A');  
            queue.inqueue(c);  
            try...{  
                Thread.sleep((int)(Math.random()*20));  
            }catch(InterruptedException e)...{System.out.println(e);}  
        }  
    }  


class Producer implements Runnable...{
    SyncQueue queue;
    public Producer(SyncQueue s)...{
        queue=s;
    }
    public void run()...{
        for(int i=0;i<20;i++)...{
            char c=(char)(Math.random()*26+'A');
            queue.inqueue(c);
            try...{
                Thread.sleep((int)(Math.random()*20));
            }catch(InterruptedException e)...{System.out.println(e);}
        }
    }
}


Java代码
class Customer implements Runnable...{  
    SyncQueue queue;  
    public Customer(SyncQueue s)...{  
        queue=s;  
    }  
    public void run()...{  
        for(int i=0;i<20;i++)...{  
            char c=queue.outqueue();  
            try...{  
                Thread.sleep((int)(Math.random()*20));  
            }catch(InterruptedException e)...{System.out.println(e);}  
        }  
    }  


class Customer implements Runnable...{
    SyncQueue queue;
    public Customer(SyncQueue s)...{
        queue=s;
    }
    public void run()...{
        for(int i=0;i<20;i++)...{
            char c=queue.outqueue();
            try...{
                Thread.sleep((int)(Math.random()*20));
            }catch(InterruptedException e)...{System.out.println(e);}
        }
    }
}


Java代码
public class SyncTest...{  
    public static void main (String[] args) ...{  
        SyncQueue queue=new SyncQueue();  
        Runnable p=new Producer(queue);  
        Runnable c=new Customer(queue);  
        Thread t1=new Thread(p);  
        Thread t2=new Thread(c);  
        t1.start();  
        t2.start();  
          
    }  


public class SyncTest...{
    public static void main (String[] args) ...{
        SyncQueue queue=new SyncQueue();
        Runnable p=new Producer(queue);
        Runnable c=new Customer(queue);
        Thread t1=new Thread(p);
        Thread t2=new Thread(c);
        t1.start();
        t2.start();
       
    }
}

注:注意wait()和notify()的位置,否则将引者死锁的发生。

分享到:
评论

相关推荐

    阻塞队列实现生产者消费者模式Java开发Java经验技巧共

    阻塞队列实现生产者消费者模式Java开发Java经验技巧共4页.pdf.zip

    java 基于队列实现的生产消费者模式

    基于队列实现的生产消费者模式java 源码,并且采用多线程进行消费

    【Java】Queue、BlockingQueue和队列实现生产者消费者模式

    源码:BlockingQueue实现生产者消费者模式→ 输出结果截图 1. Queue接口 – 队列 public interface Queue extends Collection Collection的子接口,表示队列FIFO(First In First Out) 常用方法: (1)抛出异常...

    Java实现消费者与生产者两个线程问题

    用Java实现消费者与生产者两个线程进行调度

    消息分发框架(基于JAVA阻塞队列实现、 生产者消费者模型)

    消息分发框架,基于java阻塞队列实现,生产者消费者模型 可用于任务分发,服务器消息消息,以及网络IO 性能优化,多线程

    生产者与消费者问题 java

    生产者与消费者问题 java #include #include const unsigned short SIZE_OF_BUFFER = 10; //缓冲区长度 unsigned short ProductID = 0; //产品号 unsigned short ConsumeID = 0; //将被消耗的产品号 unsigned ...

    java多线程实现生产者和消费者

    java多线程实现生产者和消费者 ,4种实现方式,分别为synchronizated,condition和lock,信号量,阻塞队列

    java模拟阻塞队列

    实现java模拟阻塞队列的例子,该代码包括,阻塞队列实现生产者,消费者。和模拟阻塞队列实现生产者及消费者模式,帮助你更好的理解java多线程

    JAVA多线程之生产者消费者模型.docx

    那么在这个过程中,生产者和消费者是不直接接触的,所谓的‘货架’其实就是一个阻塞队列,生产者生产的产品不直接给消费者消费,而是仍给阻塞队列,这个阻塞队列就是来解决生产者消费者的强耦合的。就是生产者消费者...

    java多线程例子-生产者消费者

    使用一个共享队列,生产者把产品放入队列,消费者消费产品,实现简单的多线程示例.

    rabbitmq 7种队列实现java版

    文章目录rabbitmq7种实现方式搭建maven项目引入依赖创建连接简单队列消息生产者消息消费者work queues 工作队列生产者消费者能者多劳(公平分发):消费能力强则消费更多消息Publish/Subscribe 发布订阅模式生产者...

    Java多线程 BlockingQueue实现生产者消费者模型详解

    主要介绍了Java多线程 BlockingQueue实现生产者消费者模型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    基于Java实现生产者与消费者算法模拟【100010232】

    本次课程设计选到的题目为生产者消费者算法模拟,通过需求分析和资料搜寻,掌握到生产者/消费者的模式原理和优点,同时也了解到了几种可以实现生产者消费者的方式,如信号量方式,管程方式,阻塞队列方式等。

    Java实现Kafka数据生产者

    "Java实现Kafka数据生产者"这个功能可以让你使用Java编程语言创建一个能够连接到指定的Kafka集群(通过bootstrap servers)并向其中发送数据的应用程序。通过这个功能,你可以将数据以消息的形式发送到Kafka主题中,...

    Java并发编程:阻塞队列

     使用非阻塞队列的时候有一个很大问题是:它不会对当前线程产生阻塞,那么在面对类似消费者-生产者的模型时,必须额外地实现同步策略以及线程间唤醒策略,这个实现起来非常麻烦。但是有了阻塞队列不一样了,它会

    JAVA编程之Spring boot-activeMQ示例

    分别实现生产者-消费者模式和发布-订阅模式,作为java编程发送消息和消费消息的基础示例。 源码主要包含如下内容: 1.spring boot配置初始化activeMQ 2.队列类型queue,生产者发送队列消息,以及消费者消费相关队列...

    java多线程编程总结

    Java线程:并发协作-生产者消费者模型 Java线程:并发协作-死锁 Java线程:volatile关键字 Java线程:新特征-线程池 Java线程:新特征-有返回值的线程 Java线程:新特征-锁(上) Java线程:新特征-锁(下) Java...

    如何在Java中正确使用 wait, notify 和 notifyAll

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视。本文对这些关键字的使用进行了描述。  在 Java 中可以用 wait、notify ...例如,在生产者消费者模

    【资源免费下载】Java代码积累丨大话设计模式(Java实现版本)、线程协作

    生产者-消费者 设计模式参考《大话设计模式》 工厂简单模式 创造型模式 工厂方法模式 抽象工厂模式 原型模式 建造者模式 单例模式 结构型模式 队列模式 桥接模式 组合模式 装饰模式 外观模式 享元模式 代理模式 行为...

Global site tag (gtag.js) - Google Analytics