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();
        }
    }
  • 相关阅读:
    sql server 2008数据复制方法
    排错技能:任务管理器中追踪某w3wp.exe是哪个IIS站点的application pool
    SplendidCRM中给来自EditView中的listbox控件设置选中值或数据源
    jQuery String Functions
    [转]jquery getJSON 数据联动(采用序列化和反序列化获取数据) .
    [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")
    深入理解C语言
    Qt回忆录之配置开发环境
    360电话面试
    浅谈C++设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860855.html
Copyright © 2011-2022 走看看