四、java多线程基础之线程之间的通信

一、什么是多线程之间的通讯

1.概念:多线程之间通讯,其实就是多个线程在操作同一个资源,但是操作的动作不同。

二、多线程通讯模拟出现的问题

1.需求:第一个线程写入(input)用户,另一个线程取读取(out)用户.实现读一个,写一个操作。

2.代码

// 共享对象
class Res {
    // 姓名
    public String name;
    // 性别
    public String sex;
}
// 生产这线程
class IntThread extends Thread {
    public Res res;
    public IntThread(Res res) {
        this.res = res;
    }
    @Override
    public void run() {
        int count = 0; // 1
        while (true) {
            if (count == 0) {
                res.name = "小红";
                res.sex = "女";
            } else {
                res.name = "小明";
                res.sex = "男";
            }
            count = (count + 1) % 2;// 0 1 0 1 0 1
        }
    }
}
// 读取线程
class OutThread extends Thread {
    public Res res;
    public OutThread(Res res) {
        this.res = res;
    }
    @Override
    public void run() {
        while (true) {
            System.out.println(res.name + "," + res.sex);
        }
    }
}
public class Test0001 {
    public static void main(String[] args) throws InterruptedException {
        Res res = new Res();
        IntThread intThread = new IntThread(res);
        OutThread outThread = new OutThread(res);
        intThread.start();
        outThread.start();
    }
}

3.结果

注意:数据发生错乱,造成线程安全问题

三、多线程通讯模拟解决问题

1.代码

// 共享对象
class Res {
    // 姓名
    public String name;
    // 性别
    public String sex;
    // 为true情况下 允许读,不能写
    // 为false情况下 允许写,不能读。
    public boolean flag = false;
}
// 生产这线程
class IntThread extends Thread {
    public Res res;

    public IntThread(Res res) {
        this.res = res;
    }
    @Override
    public void run() {
        int count = 0; // 1
        while (true) {
            synchronized (res) {
                if (res.flag) {
                    try {
                        res.wait();// 釋放当前锁对象
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                if (count == 0) {
                    res.name = "小红";
                    res.sex = "女";
                } else {
                    res.name = "小明";
                    res.sex = "男";
                }
                count = (count + 1) % 2;// 0 1 0 1 0 1
                res.flag = true;// 标记当前线程为等待
                res.notify();// 唤醒被等待的线程
            }
        }
    }
}
// 读取线程
class OutThread extends Thread {
    public Res res;

    public OutThread(Res res) {
        this.res = res;
    }
    @Override
    public void run() {
        while (true) {
            synchronized (res) {
                try {
                    if (!res.flag) {
                        res.wait();
                    }
                    Thread.sleep(1000);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println(res.name + "," + res.sex);
                res.flag = false;
                res.notify();
            }
        }
    }
}
public class Test0001 {
    public synchronized static void main(String[] args) throws InterruptedException {
        Res res = new Res();
        IntThread intThread = new IntThread(res);
        OutThread outThread = new OutThread(res);
        intThread.start();
        outThread.start();
    }
}

2.结果

3.分析

3.1.因为涉及到对象锁,他们必须都放在synchronized中来使用. Wait、Notify一定要在synchronized里面进行使用。

3.2.Wait必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行。

注意:一定要在线程同步中使用,并且是同一个锁的资源

四、小福利

1.wait与sleep的区别

对于sleep()方法,我们首先要知道该方法是属于Thread类中的。而wait()方法,则是属于Object类中的。

sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。

在调用sleep()方法的过程中,线程不会释放对象锁。

而当调用wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备,获取对象锁进入运行状态。

五、结束

Always keep the faith!!!

发布了122 篇原创文章 · 获赞 64 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/chenmingxu438521/article/details/103777197