前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >010.多线程-线程间通信

010.多线程-线程间通信

作者头像
qubianzhong
发布2018-12-13 16:14:34
3800
发布2018-12-13 16:14:34
举报
文章被收录于专栏:行者常至行者常至

版权声明:本文为博主原创文章,允许转载,请标明出处。

多线程之间的通信,其实就是多个线程同时操作(读+写)同一个资源。


安全问题: 当线程在读取资源的过程中,写线程操作了资源, 导致读线程读取的数据,一部分是写之前的数据,一部分是写之后的数据。


解决安全问题: 读线程和写线程使用同一把对象锁就好了。


code of demo:

代码语言:javascript
复制
package cn.qbz.thread;

/**
 * 线程间通信
 */
public class ConnectThreadTest {

    public static void main(String[] args) {
        Student student = new Student();
        ProduceTest produceTest = new ProduceTest(student);
        ConsumerTest consumerTest = new ConsumerTest(student);

        produceTest.start();
        consumerTest.start();

    }
}

class ProduceTest extends Thread {
    private Student student;

    public ProduceTest(Student student) {
        this.student = student;
    }

    @Override
    public void run() {
        int num = 1;
        while (true) {
            synchronized (student) {
                //如果可以生产,生产者生产
                if (student.getCanProduce()) {
                    if (num == 1) {
                        student.setAge(6);
                        student.setName("小王");
                        num = 0;
                    } else {
                        student.setName("老王");
                        student.setAge(99);
                        num = 1;
                    }

                    //重置生产者不可以生产
                    student.setCanProduce(false);
                }

                student.notifyAll();
                try {
                    student.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class ConsumerTest extends Thread {
    private Student student;

    public ConsumerTest(Student student) {
        this.student = student;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (student) {
                //如果不可以生产,进行消费
                if (!student.getCanProduce()) {
                    //重置生产者可以生产
                    student.setCanProduce(true);
                    System.out.println(student.toString());
                }

                student.notifyAll();
                try {
                    student.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class Student {
    private String name;
    private Integer age;
    private Boolean canProduce = false;

    public Boolean getCanProduce() {
        return canProduce;
    }

    public void setCanProduce(Boolean canProduce) {
        this.canProduce = canProduce;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档