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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周飞飞 中级黑马   /  2015-8-12 10:46  /  173 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package day12;

class Rec2 {
        private String name ;
        private String sex;
        private boolean flag;
        public synchronized void set(String name,String sex){
                if(this.flag)
                        try {this.wait();} catch (InterruptedException e) {}
                this.name = name;
                this.sex = sex;
                flag = true;
                this.notify();
               
        }
        public synchronized void get(){
                if(!this.flag)
                        try {this.wait();} catch (InterruptedException e) {}
                System.out.println(this.name+"......."+this.sex);
                flag = false;
                this.notify();
        }
}
class Input2 implements Runnable{
        private Rec2 r;
        Input2(Rec2 r){
                this.r= r;
        }
        public void run(){
                int x = 0;
                while(true){
                       
                        if(x==0)
                                r.set("zhangsan", "man");
                        else
                                r.set("李四", "女女女");
               
                        x = (x+1)%2;
                }
       
        }
}
class Output2 implements Runnable{
        private Rec2 r;
        Output2(Rec2 r){
                this.r = r;
        }
        public void run(){
                while(true){
                        r.get();
                }
        }
}
public class InputOutputDemo2 {
        public static void main(String args[]){
                Rec2 r = new Rec2();
                new Thread(new Input2(r)).start();
                new Thread(new Output2(r)).start();
        }

}

2 个回复

倒序浏览
风华正茂 来自手机 中级黑马 2015-8-12 12:41:28
沙发
谢谢楼主分享,好帖必须顶
回复 使用道具 举报
悄悄告诉大家一个好消息,这个要重点掌握哦!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马