实现java的多线程操作——线程综合实战生产者与消费者(八)

线程综合实战生产者与消费者

线程间的通讯问题及Object类的支持。现在希望实现一种数据的生产和取出的操作形式,即:有两个甚至更多的线程对象,这样的线程分为生产者线程和消费者线程。那么最理想的状态是生产者每生产一条完整的数据之后,消费者就要取走这个数据,并且进行输出的打印。

现在假设要输出的信息有这样两个:
title = 陶帅帅 content = 一个帅哥
title = 可爱的小动物,content = 草泥马

在这里插入图片描述
范例:程序的初期实现

package day1;

class Info{
    private String title;
    private String content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
class Productor implements Runnable{
    private Info info = null;
    public Productor (Info info){
        this.info = info;
    }
    @Override
    public void run() {
        for(int i = 0 ;i < 50;i++){
            if(i % 2 == 0){
                this.info.setTitle("陶帅帅");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.info.setContent("一个帅哥");
            }else {
                this.info.setTitle("可爱的小动物");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.info.setContent("草泥马");
            }
        }
    }
}
class Consumer implements Runnable{
    private Info info;
    public Consumer(Info info){
        this.info = info;
    }
    @Override
    public void run() {
        for(int i = 0 ;i < 50;i++){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.info.getTitle() + "——>" +this.info.getContent());
        }
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Info info = new Info();
        Productor p = new Productor(info);
        Consumer c = new Consumer(info);
        new Thread(p).start();
        new Thread(c).start();
    }
}
可爱的小动物——>一个帅哥
可爱的小动物——>一个帅哥
可爱的小动物——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>草泥马
可爱的小动物——>草泥马
陶帅帅——>草泥马
陶帅帅——>草泥马
陶帅帅——>草泥马
陶帅帅——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>一个帅哥
可爱的小动物——>一个帅哥
可爱的小动物——>一个帅哥
可爱的小动物——>一个帅哥
可爱的小动物——>一个帅哥
陶帅帅——>草泥马
陶帅帅——>草泥马

通过以上的执行可以发现两个问题
1.数据错位了(数据不同步);2.重复生产,重复取出

1.1 解决数据不同步问题
要想解决同步问题一定使用同步代码块或者同步方法,既然要同步,那么肯定要讲设置属性和取得的属性的内容都统一 交给Info类完成。

范例:使用同步代码块或者同步方法

package day1;

class Info{
    private String title;
    private String content;
    public synchronized void set(String title,String content){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.title = title;
        this.content = content;
    }
    public synchronized void get(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.title + "——>" +this.content);
    }
}
class Productor implements Runnable{
    private Info info = null;
    public Productor (Info info){
        this.info = info;
    }
    @Override
    public void run() {
        for(int i = 0 ;i < 50;i++){
            if(i % 2 == 0){
                this.info.set("陶帅帅","一个帅哥");
            }else {
                this.info.set("可爱的小动物","草泥马");
            }
        }
    }
}
class Consumer implements Runnable{
    private Info info;
    public Consumer(Info info){
        this.info = info;
    }
    @Override
    public void run() {
        for(int i = 0 ;i < 50;i++){
          this.info.get();
        }
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Info info = new Info();
        Productor p = new Productor(info);
        Consumer c = new Consumer(info);
        new Thread(p).start();
        new Thread(c).start();
    }
}
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
可爱的小动物——>草泥马
可爱的小动物——>草泥马
可爱的小动物——>草泥马
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
可爱的小动物——>草泥马
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
可爱的小动物——>草泥马
。。。。。。。。。。。。

所有的设置和取得数据的操作都交给了同步方法完成。
现在虽然同步的问题解决了,但是重复的操作更严重了。

1.2 解决重复问题
在这里插入图片描述

如果要想解决重复问题,那么必须加入等待与唤醒的处理机制,而这样的操作方法是由Object类所提供的。在Object类中提供有如下几种方法:
(1)等待:public final void wait() throws InterruptedException;
(2)唤醒第一个等待线程:public final void notify();
(3)唤醒全部等待线程:public final void notifyAll();

范例:修改Info类

package day1;

class Info{
    private String title;
    private String content;
    private boolean flag =true;
    //flag=true:表示可以生产数据,但是不能取走数据
    //flag=false:表示可以取走数据,但是不能生产数据
    public synchronized void set(String title,String content){
        if(this.flag == false ){//表示已经生产过了,还没有取走
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.title = title;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.content = content;
        flag = false;//表示生产过了
        super.notify();
    }
    public synchronized void get(){
        if(this.flag == true){//此时应该生产,不应该取走
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.title + "——>" +this.content);
        flag = true;//表示可以取走了
        super.notify();
    }
}
class Productor implements Runnable{
    private Info info = null;
    public Productor (Info info){
        this.info = info;
    }
    @Override
    public void run() {
        for(int i = 0 ;i < 50;i++){
            if(i % 2 == 0){
                this.info.set("陶帅帅","一个帅哥");
            }else {
                this.info.set("可爱的小动物","草泥马");
            }
        }
    }
}
class Consumer implements Runnable{
    private Info info;
    public Consumer(Info info){
        this.info = info;
    }
    @Override
    public void run() {
        for(int i = 0 ;i < 50;i++){
          this.info.get();
        }
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Info info = new Info();
        Productor p = new Productor(info);
        Consumer c = new Consumer(info);
        new Thread(p).start();
        new Thread(c).start();
    }
}
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
陶帅帅——>一个帅哥
可爱的小动物——>草泥马
。。。。。。。。。。。。。。

面试题:请解释sleep()与wait()的区别
1.sleep()是Thread类定义的方法,在休眠到一定时间之后将自己唤醒。
2.wait()是Object类定义的方法,表示线程要等待执行,必须通过notify()或者notyfyAll()方法唤醒。

总结:生产者和消费者这是个模型,完整的体现了线程的同步、Object类的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值