生产者与消费者,同步数据问题

生产者与消费者,同步数据问题

package duoxian;

class Message {
   private String tile;
   private String content;


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

    public String getTile() {
        return tile;
    }

    public String getContent() {
        return content;
    }

    public void setTile(String str) {
           this.tile=str;
    }
}

class Producer implements Runnable{
    private Message msg=null;
    public Producer(Message msg) {
        this.msg=msg;
    }

    @Override
    public synchronized void run() {
        for (int i = 0; i < 50; i++) {
            if (i%2==0){
                this.msg.setTile("麦观文");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.msg.setContent("Java讲师");
            }else{
                this.msg.setTile("mldn");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.msg.setContent("www.baidu.com");
            }
        }
    }
}

class Consumer implements Runnable {
    private Message msg=null;
    public Consumer(Message msg){
        this.msg=msg;
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.msg.getTile()+"-->"+this.msg.getContent());
        }
    }
}

在这里插入图片描述

存在两个错误:数据错位重复操作,生产者或消费者多次连续重复操作

解决数据同步问题

使用synchronized同步的方式解决“生成者-消费者”代码中出现的不同步问题

package duoxian;

class Message {
   private String tile;
   private String content;

   public synchronized void set(String tile,String content){
       this.tile=tile;
       try {
           Thread.sleep(100);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       this.content=content;
   }
   public synchronized String get(){
       try {
           Thread.sleep(100);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return this.tile+"-->"+this.content;
   }
 }

class Producer implements Runnable{
    private Message msg=null;
    public Producer(Message msg) {
        this.msg=msg;
    }

    @Override
    public  void run() {
        for (int i = 0; i < 50; i++) {
            if (i%2==0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.msg.set("麦观文","Java讲师");
            }else{
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.msg.set("maid","www.baidu.com");
            }
        }
    }
}

class Consumer implements Runnable {
    private Message msg=null;
    public Consumer(Message msg){
        this.msg=msg;
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.msg.get());
        }
    }
}

Object线程等待与呼唤

修改Message类,解决数据的重复设置和重复取出操作

使用wai(),notify(),notifyAll(),方法

package duoxian;

class Message {
    private String tile;
    private String content;
    private boolean flag=true;
    public synchronized void set(String tile,String content){
        if(this.flag==false){
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.tile=tile;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.content=content;
        this.flag=false;
        super.notify();
    }
    public synchronized String get(){

        if (this.flag==true){
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try{return this.tile+"-->"+this.content;
        }finally {
            this.flag=true;
            super.notify();
        }
    }
}


package duoxian;

public class ThreadDemo {
    public static void main(String[] args) {
      Message msg=new Message();
      new Thread(new Producer(msg)).start();
      new Thread(new Consumer(msg)).start();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值