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)
            {
                synchronized(resource)
                {
                    try
                    {
                        if(resource.getFlag())
                            resource.wait();
                        if(flag)
                        {
                            resource.setName("zhangsan");
                            resource.setSex("nan");
                        }
                        else
                        {
                            resource.setName("lishi");
                            resource.setSex("nv");
                        }
                        resource.setFlag(true);
                        resource.notify();
                        
                    }
                    catch(Exception e)
                    {
                        System.err.println(e.getMessage());
                    }
                }
                
                flag=!flag;
            }
        }
        
    }

    class Output implements Runnable
    {
        private Resource resource;
        
        public Output(Resource resource)
        {
            this.resource = resource;
        }
        
        public void run()
        {        
            while(true)
            {
                synchronized(resource)
                {
                    try
                    {
                        if(!resource.getFlag())
                            resource.wait();
                            
                        System.out.println(resource.getName() + " ... "+resource.getSex());
                        resource.setFlag(false);
                        resource.notify();
                    }
                    catch(Exception e)
                    {
                        System.err.println(e.getMessage());
                    }
                }
            }
        }
        
    }

    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;
        }
    }
  • 相关阅读:
    PAT乙级真题1004. 成绩排名 (20)(解题)
    PAT乙级真题1003. 我要通过!(20)(解题)
    PAT乙级真题1002. 写出这个数 (20)(解题)
    PAT乙级真题1001. 害死人不偿命的(3n+1)猜想 (15)(解题)
    2015-03-06——ajax基础
    2015-03-06——正则表达式基础
    2015-02-09——js笔记
    2015-02-08——js笔记
    2015-02-07——js笔记
    2014-10-28——iframe多层嵌套时获取元素总结
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860828.html
Copyright © 2011-2022 走看看