[转帖]Java 生产者与消费者_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2624 | 回复: 0   主题: [转帖]Java 生产者与消费者        下一篇 
只是很无聊
注册用户
等级:中尉
经验:440
发帖:33
精华:0
注册:2013-6-18
状态:离线
发送短消息息给只是很无聊 加好友    发送短消息息给只是很无聊 发消息
发表于: IP:您无权察看 2013-6-19 11:20:08 | [全部帖] [楼主帖] 楼主

  1. public class Info { 
  2.        private int id=0; 
  3.       
  4.        public synchronized void increace(String name,boolean flag){ 
  5.              System.out.println("准备生产。。。"); 
  6.              try { 
  7.                    Thread.sleep(600); 
  8.              } catch (InterruptedException e) { 
  9.                    e.printStackTrace(); 
  10.              } 
  11.             
  12.              while(id>0){ 
  13.                    try { 
  14.                          super.wait(); 
  15.                    } catch (InterruptedException e) { 
  16.                          e.printStackTrace(); 
  17.                    } 
  18.              } 
  19.             
  20.              System.out.println("生产中。。。"); 
  21.              try { 
  22.                    Thread.sleep(2000); 
  23.              } catch (InterruptedException e) { 
  24.                    e.printStackTrace(); 
  25.              } 
  26.              id++; 
  27.              System.out.println("产品量:"+id); 
  28.              System.out.println("唤醒消费者中(让消费者消费)。。。"); 
  29.              try { 
  30.                    Thread.sleep(2000); 
  31.              } catch (InterruptedException e) { 
  32.                    e.printStackTrace(); 
  33.              } 
  34.              notify(); 
  35.              System.out.println(name+"生产成功。。。"); 
  36.        } 
  37.       
  38.        public synchronized void decreace(String name,boolean flag){ 
  39.              System.out.println("准备消费。。。"); 
  40.              try { 
  41.                    Thread.sleep(600); 
  42.              } catch (InterruptedException e) { 
  43.                    e.printStackTrace(); 
  44.              } 
  45.             
  46.              while(id<=0){ 
  47.                    try { 
  48.                          super.wait(); 
  49.                    } catch (InterruptedException e) { 
  50.                          e.printStackTrace(); 
  51.                    } 
  52.              } 
  53.              System.out.println("消费中。。。"); 
  54.              try { 
  55.                    Thread.sleep(2000); 
  56.              } catch (InterruptedException e) { 
  57.                    e.printStackTrace(); 
  58.              } 
  59.              id--; 
  60.              System.out.println("产品量:"+id); 
  61.              System.out.println("唤醒生产者中(等待生产者生产)。。。"); 
  62.              notify(); 
  63.              System.out.println(name+"消费成功。。。"); 
  64.        } 



Java代码  
北京联动北方科技有限公司

  1. public class Producer implements Runnable{ 
  2.       
  3.        private Info info; 
  4.        private String name; 
  5.        public Producer(String name,Info info){ 
  6.              this.name = name; 
  7.              this.info = info; 
  8.        } 
  9.       
  10.        public void run() { 
  11.              while(true){ 
  12.                    info.increace(name,true); 
  13.              } 
  14.        } 



Java代码  
北京联动北方科技有限公司

  1. public class Consumer implements Runnable{ 
  2.       
  3.        private Info info; 
  4.        private String name; 
  5.        public Consumer(String name,Info info){ 
  6.              this.name = name; 
  7.              this.info = info; 
  8.        } 
  9.       
  10.        public void run() { 
  11.              while(true){ 
  12.                    info.decreace(name,false); 
  13.              } 
  14.        } 



Java代码  
北京联动北方科技有限公司

  1. public class PCThreadTest { 
  2.       
  3.       
  4.        public static void main(String[] args) { 
  5.              Info info = new Info(); 
  6.              Producer producer = new Producer("生产者1",info); 
  7.              Consumer consumer = new Consumer("消费者1",info); 
  8.              Thread t1 = new Thread(producer); 
  9.              Thread t2 = new Thread(consumer); 
  10.              t1.start(); 
  11.              t2.start(); 
  12.        } 




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论