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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 无道 中级黑马   /  2014-1-7 22:39  /  2135 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

代码如下:
  1. package thread;
  2. import java.util.*;
  3. import java.util.concurrent.* ;



  4. //resource
  5. class Resource{
  6.        
  7.                  private String name;
  8.                  private int id;
  9.                  private boolean falg;
  10.                  private Lock lock = new ReentrantLock();
  11.                  private  Condition con_pro = lock.newCondition();
  12.                  private Condition con_con = lock.newCondition();
  13.                 
  14.         public void setName(String name){
  15.                
  16.                
  17.                 this.name = name;       
  18.                
  19.         }
  20.         public void setId(int id){
  21.                
  22.                 this.id = id ;       
  23.                
  24.         }
  25.         public void setInfo(String name)throws InterruptedException{
  26.                
  27.                 lock.lock();try{
  28.     while(falg){
  29.                                
  30.                                         con_pro.await();                               
  31.                                 }
  32.                                
  33.                
  34.                
  35.                 this.name = name;
  36.                 this.id = id;
  37.                 falg = true;
  38.                 con_con.signal();       
  39.                 }       
  40.                         finally{
  41.                                        
  42.                                         lock.unLock();       
  43.                                 }
  44.         }
  45.        
  46.         public void getInof(){
  47.                
  48.                 lock.lock();
  49.                
  50.                 while(!falg){
  51.                        
  52.                         try{
  53.                                
  54.                                         con_con.await();
  55.                         }
  56.                         finally{
  57.                                
  58.                                 lock.unLock();
  59.                                                        
  60.                         }
  61.                                
  62.                 }
  63.                 System.out.println(Thread.currentThread().getName()+"Name:"+name+"+Id:"+id);       
  64.                 id++;
  65.         }
  66. }


  67. class Producer implements Runnable{

  68.         private Resource res;
  69.        
  70.         Producer(Resource res){
  71.                
  72.                 this.res = res;       
  73.         }       
  74.        
  75.         public void run(){
  76.                         while(true){
  77.                        
  78.                                 try{
  79.                                        
  80.                                                 res.setInfo("产品");       
  81.                                 }       
  82.                                 catch(InterruptedException e){
  83.                                        
  84.                                         e.printlStackTrace();
  85.                                                
  86.                                 }                       
  87.                                
  88.                         }
  89.         }
  90. }

  91. class Concumter implements Runnable{
  92.        
  93.         private Resource res;
  94.        
  95.         Concumter(Resource res){
  96.        
  97.                 this.res = res;       
  98.         }       
  99.        
  100.         public void run(){
  101.                
  102.                 while(true){
  103.                        
  104.                         res.getInfo();       
  105.                 }
  106.                        
  107.         }
  108. }

  109. public class ProducerLock{
  110.        
  111.         public static void main(String [] args){
  112.                
  113.                 Resource res = new Resource();
  114.                
  115.                 new Thread(new Producer(res)).start();
  116.                 new Thread(new Producer(res)).start();
  117.                 new Thread(new Consumer(res)).start();
  118.                 new Thread(new Consumer(res)).start();       
  119.         }       
  120. }
复制代码
此代码还有一些别的问题,我就想知道为什么编译会提示找不到Condition类

ll.jpg (103.51 KB, 下载次数: 79)

ll.jpg

6 个回复

倒序浏览
Condition是java.util.concurrent.locks.*;

这个问题多注意吧.包下的子包是不会在用*导入时导入的.
有子包的话,要明确到子包哦.

有点内部类访问的那种味道...自己品品~
回复 使用道具 举报
高亮亮 发表于 2014-1-8 16:04
Condition是java.util.concurrent.locks.*;

这个问题多注意吧.包下的子包是不会在用*导入时导入的.

如果使用import java.util.*;导入,我试过也不行呀,
回复 使用道具 举报
无道 发表于 2014-1-11 22:05
如果使用import java.util.*;导入,我试过也不行呀,

concurrent.被你吃了...
回复 使用道具 举报
是不是我的虚拟机缺少一个jar包呀!希望能在你们的机子上运行一下,证实一下,谢谢。
回复 使用道具 举报
刚提交的入学申请,看谈出来一个新消息,以为审核了,害我白激动一场啊
请你看信息,看仔细.到以后码代码不仔细,会返工很多东西滴!~
回复 使用道具 举报


import java.util.*;
import java.util.concurrent.* ;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;



//resource
class Resource{
        
                 private String name;
                 private int id;
                 private boolean falg;
                 private Lock lock = new ReentrantLock();
                 private  Condition con_pro = lock.newCondition();
                 private Condition con_con = lock.newCondition();
                 
        public void setName(String name){
               
               
                this.name = name;        
               
        }
        public void setId(int id){
               
                this.id = id ;        
               
        }
        public void setInfo(String name)throws InterruptedException{
               
                lock.lock();try{
    while(falg){
                                
                                        con_pro.await();                                
                                }
                                
               
               
                this.name = name;
                this.id = id;
                falg = true;
                con_con.signal();        
                }        
                        finally{
                                       
                                        lock.unlock();        
                                }
        }
        
        public void getInof() throws InterruptedException {
               
                lock.lock();
               
                while(!falg){
                        
                        try{
                                
                                        con_con.await();
                        }
                        finally{
                                
                                lock.unlock();
                                                        
                        }
                                
                }
                System.out.println(Thread.currentThread().getName()+"Name:"+name+"+Id:"+id);        
                id++;
        }
}


class Producer implements Runnable{

        private Resource res;
        
        Producer(Resource res){
               
                this.res = res;        
        }        
        
        public void run(){
                        while(true){
                        
                                try{
                                       
                                                res.setInfo("产品");        
                                }        
                                catch(InterruptedException e){
                                       
                                        e.printStackTrace();
                                                
                                }                        
                                
                        }
        }
}

class Concumter implements Runnable{
        
        private Resource res;
        
        Concumter(Resource res){
        
                this.res = res;        
        }        
        
        public void run(){
               
                while(true){
                        
                        try {
                                                        res.getInof();
                                                } catch (InterruptedException e) {
                                                        // TODO 自动生成的 catch 块
                                                        e.printStackTrace();
                                                }        
                }
                        
        }
}

public class ProducerLock{
        
        public static void main(String [] args){
               
                Resource res = new Resource();
               
                new Thread(new Producer(res)).start();
                new Thread(new Producer(res)).start();
                new Thread(new Concumter(res)).start();
                new Thread(new Concumter(res)).start();        
        }        
}
这是我改过的 现在没问题了   导入了哪些包你自己看 还有就是你的代码中有些大小写错误改改
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马