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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宋蕈 中级黑马   /  2012-4-9 09:59  /  1634 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Info{
        private String name;
        private String content;
        private boolean flag=false;
        public synchronized void set(String name,String content){
                this.setName(name);
                try{
                        Thread.sleep(100);
                }catch(InterruptedException e){
                        e.printStackTrace();
                }
                this.setContent(content);

                flag=true;
        }

        public synchronized void get(){
                try{
                                Thread.sleep(100);
                        }catch(InterruptedException e){
                                e.printStackTrace();
                        }
                        System.out.println(this.getName()+"--->"+this.getContent());

                flag=false;
        }
        public void setName(String name){
                this.name=name;
        }

        public String getName(){
                return this.name;
        }

        public void setContent(String content){
                this.content=content;
        }

        public String getContent(){
                return this.content;
        }
}

class Producer implements Runnable{
        private boolean flag=false;
        private Info info;
        public Producer(Info info){
                this.info=info;
        }

        public void run(){
               
                for(int i=0;i<50;i++){

                                if(flag){                               
                                                this.info.set("Tom","JAVA讲师");
                                                flag=false;                                               
                                }else{
                                                this.info.set("Jack","C++讲师");
                                                flag=true;
                                }                       
                       
                }
        }
}

class Consumer implements Runnable{
        private Info info;

        public Consumer(Info info){
                this.info=info;
        }

        public void run(){
                for(int i=0;i<50;i++){
                        this.info.get();
                }
        }
}

class ThreadTest_02{
        public static void main(String[] args){
                Info info=new Info();
                Producer p=new Producer(info);
                Consumer c=new Consumer(info);
                new Thread(p).start();
                new Thread(c).start();
        }
}

这里我想实现 Tom --> java讲师  和Jack --> C++讲师  这两个一样输出一句,怎么实现啊。?
它上面好像就只能 阵输出 Tom --> java讲师  , 要不然就是一阵输出 Jack --> C++讲师。。

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

2 个回复

倒序浏览
  给set 和get 方法中各添加  wait  notify  就哦了    视频中的 线程等待唤醒机制 那块就会讲  你应该马上就看到了
回复 使用道具 举报
让人看完后就要往上顶往死里顶的好帖
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马