美文网首页
21 多线程

21 多线程

作者: 滔滔逐浪 | 来源:发表于2020-07-17 09:59 被阅读0次

多和线程多听一个对象实现,实现不同的操作,过程可以称作为多线程之间实现通讯

package com.taotao.zuoye.thread;

/**
 *@author tom
 *Date  2020/7/17 0017 9:38
 *多线程之间的通讯
 * 多和线程多听一个对象实现,实现不同的操作,过程可以称作为多线程之间实现通讯
 */
public class Thread007 {
    class Res {
        public String userName;
        public char userSex;
        public boolean flag;
    }

    class InputThread extends Thread {

        private Res res;

        public InputThread(Res res) {
            this.res = res;
        }

        @Override
        public void run() {
            try {
                int count = 0;
                while (true) {
                    synchronized (res) {
                        if (this.res.flag) {
                            res.wait();
                        }
                        if (count == 0) {
                            this.res.userName = "22";
                            this.res.userSex = '女';
                        } else {
                            this.res.userName = "333";
                            this.res.userSex = '男';
                        }
                        this.res.flag = true;
                        this.res.notify();
                    }
                    count = (count + 1) % 2;
                }
            } catch (Exception e) {

            }
        }
    }

    class OutThread extends Thread {

        private Res res;

        public OutThread(Res res) {
            this.res = res;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    synchronized (res) {
                        if (!res.flag) {
                            res.wait();
                        }
                        System.out.println(res.userName + "," + res.userSex);
                        res.flag = false;
                        res.notify();
                    }
                } catch (Exception e) {

                }

            }
        }
    }

    public static void main(String[] args) {
        new Thread007().start();
    }

    private void start() {
        Res res = new Res();
        InputThread inputThread = new InputThread(res);
        OutThread outThread = new OutThread(res);
        inputThread.start();
        outThread.start();
    }


}


相关文章

  • Java多线程问题总结

    前言 Java多线程分类中写了21篇多线程的文章,21篇文章的内容很多,个人认为,学习,内容越多、越杂的知识,越需...

  • 40个Java多线程面试题总结

    前言 Java多线程分类中写了21篇多线程的文章,21篇文章的内容很多,个人认为,学习,内容越多、越杂的知识,越需...

  • 40个Java多线程问题总结(转载)

    前言 Java多线程分类中写了21篇多线程的文章,21篇文章的内容很多,个人认为,学习,内容越多、越杂的知识,越需...

  • 21 多线程

    多和线程多听一个对象实现,实现不同的操作,过程可以称作为多线程之间实现通讯

  • ios-interview

    title: ios-interviewdate: 2017-03-27 18:21:48tags: 一、多线程及...

  • iOS-21-多线程

    3种线程对比: NSThread: GCD: NSOperation: 常见的锁:放在子线程内部,如果放在外面相当...

  • 底层21:多线程-锁

    iOS中的线程同步方案: 1.OSSpinLock: 叫做“自旋锁”,等待锁的线程会处于忙等(busy-wait)...

  • 第21章 多线程

    1. 一个任务可以多次获得对象上的锁。 如果一个方法在同一个对象上调用了第二个方法,后者又调用了同一个对象上的另...

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

网友评论

      本文标题:21 多线程

      本文链接:https://www.haomeiwen.com/subject/itwwhktx.html