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;
        }
    }
  • 相关阅读:
    Flutter第一个应用--踩坑之路
    今天注册博客园了!
    广深小龙-基于unittest、pytest自动化测试框架之demo来学习啦!!!
    python接口自动化10-excel设计模式实战
    python接口自动化9-ddt数据驱动
    Docker学习4-学会如何让容器开机自启服务【坑】
    pytest-4-分布式运行与自定义顺序执行用例
    Docker学习10-docker-slenium进行web自动化测试
    linux+jenkins生成测试报告及任意IP打开链接能看到allure报告
    MySQL-Python实现-测试/生产环境各个表与字段进行对比的小工具
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860828.html
Copyright © 2011-2022 走看看