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();
        }
    }
  • 相关阅读:
    车载OS盘点及特点分析一:车载OS几大系统介绍
    CTF常用软件/工具
    汽车软件产业研究报告(2020年)
    高级加密标准(AES)分析
    工具 | CTP、SimNow、NSight、快期
    CTF之图片隐写术解题思路
    V2X和车路协同研究:5G V2X将成为数字座舱标配
    腾讯安全正式发布《IoT安全能力图谱》
    Microsoft Remote Desktop Beta 下载地址
    密码学初探|加密模式
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860855.html
Copyright © 2011-2022 走看看