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();
        }
    }
  • 相关阅读:
    使用runOnUiThread更新UI
    Leetcode Symmetric Tree
    EBS 开发中如何动态启用和禁止请求(Current Request)的参数
    c 陷阱与缺陷(一)
    钟浩荣战胜病魔,不负众望重踏传智播客!
    【原创】分布式之elk日志架构的演进
    【强烈谴责】博客园园友随意抄袭他人文章并作为自己原创的行为
    【原创】研发应该懂的binlog知识(下)
    【原创】研发应该懂的binlog知识(上)
    【原创】一个线程oom,进程里其他线程还能运行吗?
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860855.html
Copyright © 2011-2022 走看看