java生产者消费者

news/2024/7/4 1:31:15 标签: java, thread, buffer, class, c, string
cle class="tags" href="/tags/CLASS.html" title=class>class="baidu_pl">
cle_content" class="tags" href="/tags/CLASS.html" title=class>class="article_content clearfix">
content_views" class="tags" href="/tags/CLASS.html" title=class>class="htmledit_views">

class="tags" href="/tags/CLASS.html" title=class>class SyncStack{ //同步堆栈类
   private int index = 0; //堆栈指针初始值为0
   private char []class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/BUFFER.html" title=buffer>buffer = new char[6]; //堆栈有6个字符的空间

   public synchronized void push(char c){ //加上互斥锁
     while(index = = class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/BUFFER.html" title=buffer>buffer.length){ //堆栈已满࿰c;不能压栈
     try{
        this.wait(); //等待࿰c;直到有数据出栈
       }catch(InterruptedException e){}
       }

   this.notify(); //通知其它线程把数据出栈
   class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/BUFFER.html" title=buffer>buffer[index] = c; //数据入栈
   index++; //指针向上移动
   }

   public synchronized char pop(){ //加上互斥锁
       while(index ==0){ //堆栈无数据࿰c;不能出栈
        try{
           this.wait(); //等待其它线程把数据入栈
        }catch(InterruptedException e){}
          }

       this.notify(); //通知其它线程入栈
       index- -; //指针向下移动
       return class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/BUFFER.html" title=buffer>buffer[index]; //数据出栈
    }
       }

    class="tags" href="/tags/CLASS.html" title=class>class Producer implements Runnable{ //生产者类
       SyncStack theStack;
        //生产者类生成的字母都保存到同步堆栈中

       public Producer(SyncStack s){
          theStack = s;
       }

       public void run(){
          char c;
          for(int i=0; i<20; i++){
            c =(char)(Math.random()*26+'A');
                          //随机产生20个字符
            theStack.push(c); //把字符入栈
            System.out.println("Produced: "+c); //打印字符
            try{
            Thread.sleep((int)(Math.random()*1000));
                     /*每产生一个字符线程就睡眠*/
            }catch(InterruptedException e){}
          }
       }
     }

     class="tags" href="/tags/CLASS.html" title=class>class Consumer implements Runnable{ //消费者类
         SyncStack theStack;
                  //消费者类获得的字符都来自同步堆栈

         public Consumer(SyncStack s){
             theStack = s;
         }

         public void run(){
             char c;
             for(int i=0;i<20;i++){
               c = theStack.pop(); //从堆栈中读取字符
             System.out.println("Consumed: "+c);
                             //打印字符
             try{
             Thread.sleep((int)(Math.random()*1000));
                    /*每读取一个字符线程就睡眠*/
             }catch(InterruptedException e){}
         }
       }
     }

     public class="tags" href="/tags/CLASS.html" title=class>class SyncTest{
       public static void main(String args[]){
         SyncStack stack = new SyncStack();
   //下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
         Runnable source=new Producer(stack);
         Runnable sink = new Consumer(stack);
         Thread t1 = new Thread(source); //线程实例化
         Thread t2 = new Thread(sink); //线程实例化
         t1.start(); //线程启动
         t2.start(); //线程启动
       }
     }

cle>

http://www.niftyadmin.cn/n/1538024.html

相关文章

MySQL导入导出表数据

原文链接&#xff1a;http://blog.163.com/yang_jianli/blog/static/1619900062010111011041228/ 1、这里的导出和mysqldump不同&#xff0c;只是导出表里的纯数据&#xff0c;而不是导出为sql语句&#xff0c;可以在没有备份权限时使用。 select 列1,列2, ....... 列n from …

20135315-信息安全系统设计基础第五周学习总结

第三章 程序的机器级表示 3.1 X86 寻址方式经历三代&#xff1a; DOS时代的平坦模式&#xff0c;不区分用户空间和内核空间&#xff0c;很不安全。8060的分段模式IA32的带保护模式的平坦模式 3.2 程序编程 1、代码含义 gcc -01 -o p p1.c -01 表示使用第一级优化。优化的级别与…

Servlet中的Listener的应用

由于工作需要,最近在找一些解决方案,发现Listener是一个很好的东西, 能够监听到session,application的create,destroy,可以监听到session,application 属性绑定的变化,考虑了一下,可以应用在"在线人数统计","数据缓存"等各个方面, 下面是整理的一些资料. …

Oracle数据库笔试题(附答案)

Oracle数据库笔试题&#xff08;附答案&#xff09;2008年04月25日 星期五 19:481. 数据库切换日志的时候&#xff0c;为什么一定要发生检查点&#xff1f;这个检查点有什么意义&#xff1f;答:触发dbwr的执行&#xff0c;dbwr会把和这个日志相关的所有脏队列写到数据文件里&am…

java 最大公约数

/** *Description:greatest common divisor *Author:yemoo 2006.12.06 */ public class Pt32{ // 思路&#xff1a;辗转相除法 int divisor1( int m, int n){ // 方法一&#xff1a;循环法 int temp; if (m < n){ // if m&…

cocoaPods 的安装和使用

参考&#xff1a;http://www.360doc.com/content/14/0309/10/11029609_358970353.shtml转载于:https://www.cnblogs.com/handsomeBoys/p/4869827.html

Hibernate 关联映射总结

Hibernate 关联映射总结关键字: hibernate 关联映射 关联映射的本质&#xff1a; * 将关联关系映射到数据库&#xff0c;所谓的关联关系是对象模型在内存中的一个或多个引用 多对一&#xff08;many-to-one&#xff09; <many-to-one>会在多的一端加入一个外键&#xff0…

Hibernate缓存机制 2之数据缓存

Hibernate缓存机制 2之数据缓存2009年12月7日23:47:51对于Hibernate这类ORM而言,缓存显的尤为重要,它是持久层性能提升的关键.简单来讲Hibernate就是对JDBC进行封装,以实现内 部状态的管理,OR关系的映射等,但随之带来的就是数据访问效率的降低,和性能的下降,而缓存就是弥补这一…