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;
        }
    }
  • 相关阅读:
    笔试:一个逻辑题
    jmeter,学这些重点就可以了
    性能测试:通过一个案例(等待锁超时)告诉你,性能到底要不要熟悉业务逻辑?
    源码解读:webdriver client的原理 (面试自动化:如果你认为知道18种定位方式就算会自动化,那就太low了)
    测试必备:jmeter测试http协议接口的各种传参方式
    Vue笔记:封装 axios 为插件使用
    Vue笔记:使用 axios 发送请求
    Tomcat笔记:Tomcat的执行流程解析
    Git笔记:Git介绍和常用命令汇总
    Spring Boot使用Shiro实现登录授权认证
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860828.html
Copyright © 2011-2022 走看看