zoukankan      html  css  js  c++  java
  • Java基础多线程间通讯示例操作(已优化)二:

    class ThreadCommunicationDemo
    {
        public static void main(String[] args)
        {
            Resource resource = new Resource();
            
            Input input = new Input(resource);
            Output output = new Output(resource);
            
            new Thread(input,"input thread").start();
            new Thread(output,"output thread").start();        
        }
    }

    class Input implements Runnable
    {
        private Resource  resource;
        private boolean flag;
        public Input(Resource resource)
        {
            this.resource = resource;
        }
        
        public void run()
        {
            while(true)
            {
                if(flag)
                {
                    resource.input("zhangsan","nan");
                }
                else
                {
                    resource.input("lishi","nv");
                }
                
                flag=!flag;
            }
        }
        
    }

    class Output implements Runnable
    {
        private Resource resource;
        
        public Output(Resource resource)
        {
            this.resource = resource;
        }
        
        public void run()
        {        
            while(true)
            {
                resource.output();            
            }
        }
        
    }

    class Resource
    {
        private String Name;
        private String Sex;
        private boolean Flag;
        
        public String getName()
        {
            return this.Name;
        }
        
        public void setName(String Name)
        {
            this.Name = Name;
        }
        
        public String getSex()
        {
            return this.Sex;
        }
        
        public void setSex(String Sex)
        {
            this.Sex = Sex;
        }
        
        public Boolean getFlag()
        {
            return this.Flag;
        }
        
        public void setFlag(boolean Flag)
        {
            this.Flag = Flag;
        }
        
        public synchronized void input(String Name,String Sex)
        {
            if(this.getFlag())
                try{this.wait();}catch(Exception e){System.err.println(e.getMessage());}
                
            this.setName(Name);
            this.setSex(Sex);
            this.setFlag(true);        
            this.notify();
        }
        
        public synchronized void output()
        {
            if(!this.getFlag())
                try{this.wait();}catch(Exception e){System.err.println(e.getMessage());}
            
            System.out.println(this.getName() + " ... "+this.getSex());
            this.setFlag(false);
            this.notify();
        }
    }
  • 相关阅读:
    Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式
    Spring Boot2 系列教程(一)纯 Java 搭建 SSM 项目
    Python 解析XML实例(xml.sax)
    深度学习Tensorflow相关书籍推荐和PDF下载
    气象netCDF数据可视化分析
    Python 操作MySQL 数据库
    用这个库 3 分钟实现让你满意的表格功能:Bootstrap-Table
    浅谈压缩感知(二十六):压缩感知重构算法之分段弱正交匹配追踪(SWOMP)...
    web开发工具flask中文英文书籍-持续更新
    Python 在气象上的应用
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860855.html
Copyright © 2011-2022 走看看