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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 途中ms前进 中级黑马   /  2015-5-29 23:10  /  655 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.concurrent.locks.*;
public class Test {
       

                public static void main(String[] args){
                        res a=new res();
                        Pru p=new Pru(a);
                        Con c=new Con(a);
                        Thread t1=new Thread(p);
                        Thread t2=new Thread(c);
                        Thread t3=new Thread(p);
                        Thread t4=new Thread(c);
                }
        }
        class res{
                private String name;
                private int count=1;
                private boolean flag=false;
                private Lock lock=new ReentrantLock();
                private Condition condition_p=lock.newCondition();
                private Condition condition_c=lock.newCondition();
                res(String name){
                        this.name=name+"..."+count++;
                }
                public void set(String name)throws InterruptedException{
                        lock.lock();
                        try{while(flag)
                                condition_p.await();
                                System.out.println(Thread.currentThread().getName()+"生产者:::"+this.name);
                                condition_c.signalAll();
                       
                                }
                        finally{
                                lock.unlock();
                        }
                       
                }
                public void out()throws InterruptedException{
                        lock.lock();
                        try{while(!flag)
                                condition_c.signal();
                        System.out.println(Thread.currentThread().getName()+"消费者:::"+this.name);
                                condition_p.signalAll();
                        }
                        finally{
                                lock.unlock();
                        }
                }
                       
        }

        class Pru implements Runnable{
                private res r;
                Pru(res r){
                        this.r=r;
                }
                public void run(){
                        try{while(true)
                                r.set("商品");
                               
                        }
                        catch(Exception e){
                               
                        }
                }
               
        }
        class Con implements Runnable{
                private res r;
                Con(res r){
                        this.r=r;
                }
                public void run(){
                        try{while(true)
                                r.out();
                               
                        }
                        catch(Exception e){
                               
                        }
                }
                Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        The constructor reas() is undefined

        at itheima.Test.main(Test.java:7)

        }//为啥在Eclipse上会出现这个问题 求大神解释




12 个回复

倒序浏览
飘过学习学习
回复 使用道具 举报
新来的,在摸索中学习!
回复 使用道具 举报
代码第7行 reas()没有定义
回复 使用道具 举报
额   哪里蹦出来的reas ()  我只看到了res ()  a   ~~~~
回复 使用道具 举报
我的编辑器也经常这样   试试百度瞧瞧
回复 使用道具 举报
我的编辑器也经常这样   试试百度瞧瞧
回复 使用道具 举报
夏尔 中级黑马 2015-5-30 21:18:57
8#
你这线程都没启动。
回复 使用道具 举报
刚看到这里,不是要start才能运行么?
回复 使用道具 举报
只定义了有参数的构造函数,没定义空参数的构造函数
回复 使用道具 举报
  Thread t1=new Thread(p).statrt()。
回复 使用道具 举报
兄弟,首先你的代码有两个问题,
一,没有start(),没有启动线程
二,System.out.println(Thread.currentThread().getName()+"生产者:::"+this.name);
                                condition_c.signalAll(); 这个代码应该放在try外面。
回复 使用道具 举报
线程没启动 而且是不是包名和工程名字不一样  你再看看
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马