A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 姚志远 中级黑马   /  2013-6-16 17:44  /  1311 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 巩建 于 2013-6-18 20:43 编辑
  1. <p><strong><font color="lime">//这里为什么没有东西输出呢? 错误在哪里 求解释?</font></strong></p><p>package com.heima.exercise;
  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;

  5. //资源
  6. class Resource1{
  7.         private String name;
  8.         boolean flag=false;
  9.         private Lock lock=new ReentrantLock();
  10.         private Condition pre_condition =lock.newCondition();
  11.         private Condition cus_condition=lock.newCondition();
  12.         //生产者的方法
  13.         public void set(String name) throws Exception{
  14.                 lock.lock();
  15.                 try{
  16.                                 while(flag){
  17.                                 pre_condition.await();
  18.                                 this.name=name;
  19.                                 System.out.println(Thread.currentThread().getName()+"生产者:"+this.name);
  20.                                 this.flag=true;
  21.                                         cus_condition.signal();
  22.                         }
  23.                 }finally{lock.unlock();}
  24.         }
  25.         //消费者的方法
  26.         public void out() throws Exception{
  27.                 try{
  28.                         lock.lock();
  29.                         while(!flag){
  30.                                 cus_condition.await();
  31.                                 System.out.println(Thread.currentThread().getName()+"消费者:"+this.name);
  32.                                 this.flag=false;
  33.                                 pre_condition.signal();
  34.                         }
  35.                 }finally{lock.unlock();}
  36.         }
  37. }

  38. //生产者
  39. class Producer implements Runnable{
  40.         private Resource1 r;
  41.         public Producer(Resource1 r) {
  42.                 this.r=r;
  43.         }
  44.    public void run() {
  45.                  while(true){
  46.                          try {r.set("营养品");} catch (Exception e) {e.printStackTrace();}
  47.                  }
  48.         }
  49.   }
  50. //消费者
  51. class Customer implements Runnable{
  52.         private Resource1 r;
  53.         public Customer(Resource1 r){
  54.                 this.r=r;
  55.         }
  56.         public void run() {
  57.                  while(true){
  58.                         try {r.out();} catch (Exception e) {e.printStackTrace();}
  59.                  }
  60.   }
  61. }
  62. public class NewDemo {
  63.         public static void main(String[] args) {
  64.                 Resource1 r=new Resource1();
  65.                 Producer p=new Producer(r);
  66.                 Customer c=new Customer(r);
  67.                 Thread t1=new Thread(p);
  68.                 Thread t2=new Thread(p);
  69.                 Thread t3=new Thread(c);
  70.                 Thread t4=new Thread(c);
  71.                 t1.start();
  72.                 t2.start();
  73.                 t3.start();
  74.                 t4.start();
  75.         }

  76. }
  77. </p>
复制代码

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

3 个回复

倒序浏览
  1. lock.lock();
  2.                 try
  3.                 {
  4.                         while(flag)
  5.                                 pre_condition.await();
  6.                         this.name=name;
  7.                         System.out.println(Thread.currentThread().getName()+"生产者:"+this.name);
  8.                         this.flag=true;
  9.                         cus_condition.signal();
  10.                         
  11.                 }finally{lock.unlock();}
复制代码
楼主的循环主体定义错误,这个是我改过以后的。如果按照楼主那样定义的话,则在激活另一个线程之前,没有改变falg指针的值,那样另一个线程的主体循环永远判断为false,而同样不能激活已经被冻结的线程。这就造成了所有线程都处于冻结状态。

希望可以帮到楼主。

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

回复 使用道具 举报

RE: 关于新特性Lock锁的探究

王磊 发表于 2013-6-16 22:25
楼主的循环主体定义错误,这个是我改过以后的。如果按照楼主那样定义的话,则在激活另一个线程之前,没有改 ...

谢谢磊磊同学了 :handshake
回复 使用道具 举报

客气,一起努力吧;P
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马