zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:线程操作案例——生产者与消费者

    class Info{    // 定义信息类
        private String name = "李兴华";     // 定义name属性
        private String content = "JAVA讲师"  ;        // 定义content属性
        public void setName(String name){
            this.name = name ;
        }
        public void setContent(String content){
            this.content = content ;
        }
        public String getName(){
            return this.name ;
        }
        public String getContent(){
            return this.content ;
        }
    };
    class Producer implements Runnable{    // 通过Runnable实现多线程
        private Info info = null ;        // 保存Info引用
        public Producer(Info info){
            this.info = info ;
        }
        public void run(){
            boolean flag = false ;    // 定义标记位
            for(int i=0;i<50;i++){
                if(flag){
                    this.info.setName("李兴华") ;    // 设置名称
                    try{
                        Thread.sleep(90) ;
                    }catch(InterruptedException e){
                        e.printStackTrace() ;
                    }
                    this.info.setContent("JAVA讲师") ;    // 设置内容
                    flag = false ;
                }else{
                    this.info.setName("mldn") ;    // 设置名称
                    try{
                        Thread.sleep(90) ;
                    }catch(InterruptedException e){
                        e.printStackTrace() ;
                    }
                    this.info.setContent("www.mldnjava.cn") ;    // 设置内容
                    flag = true ;
                }
            }
        }
    };
    class Consumer implements Runnable{
        private Info info = null ;
        public Consumer(Info info){
            this.info = info ;
        }
        public void run(){
            for(int i=0;i<50;i++){
                try{
                    Thread.sleep(90) ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
                System.out.println(this.info.getName() + 
                    " --> " + this.info.getContent()) ;
            }
        }
    };
    public class ThreadCaseDemo01{
        public static void main(String args[]){
            Info info = new Info();    // 实例化Info对象
            Producer pro = new Producer(info) ;    // 生产者
            Consumer con = new Consumer(info) ;    // 消费者
            new Thread(pro).start() ;
            new Thread(con).start() ;
        }
    };
    class Info{    // 定义信息类
        private String name = "李兴华";     // 定义name属性
        private String content = "JAVA讲师"  ;        // 定义content属性
        public synchronized void set(String name,String content){
            this.setName(name) ;    // 设置名称
            try{
                Thread.sleep(300) ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            this.setContent(content) ;    // 设置内容
        }
        public synchronized void get(){
            try{
                Thread.sleep(300) ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            System.out.println(this.getName() + 
                " --> " + this.getContent()) ;
        }
        public void setName(String name){
            this.name = name ;
        }
        public void setContent(String content){
            this.content = content ;
        }
        public String getName(){
            return this.name ;
        }
        public String getContent(){
            return this.content ;
        }
    };
    class Producer implements Runnable{    // 通过Runnable实现多线程
        private Info info = null ;        // 保存Info引用
        public Producer(Info info){
            this.info = info ;
        }
        public void run(){
            boolean flag = false ;    // 定义标记位
            for(int i=0;i<50;i++){
                if(flag){
                    this.info.set("李兴华","JAVA讲师") ;    // 设置名称
                    flag = false ;
                }else{
                    this.info.set("mldn","www.mldnjava.cn") ;    // 设置名称
                    flag = true ;
                }
            }
        }
    };
    class Consumer implements Runnable{
        private Info info = null ;
        public Consumer(Info info){
            this.info = info ;
        }
        public void run(){
            for(int i=0;i<50;i++){
                this.info.get() ;
            }
        }
    };
    public class ThreadCaseDemo02{
        public static void main(String args[]){
            Info info = new Info();    // 实例化Info对象
            Producer pro = new Producer(info) ;    // 生产者
            Consumer con = new Consumer(info) ;    // 消费者
            new Thread(pro).start() ;
            new Thread(con).start() ;
        }
    };
    class Info{    // 定义信息类
        private String name = "李兴华";     // 定义name属性
        private String content = "JAVA讲师"  ;        // 定义content属性
        private boolean flag = false ;    // 设置标志位
        public synchronized void set(String name,String content){
            if(!flag){
                try{
                    super.wait() ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
            }
            this.setName(name) ;    // 设置名称
            try{
                Thread.sleep(300) ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            this.setContent(content) ;    // 设置内容
            flag  = false ;    // 改变标志位,表示可以取走
            super.notify() ;
        }
        public synchronized void get(){
            if(flag){
                try{
                    super.wait() ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
            }
            try{
                Thread.sleep(300) ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            System.out.println(this.getName() + 
                " --> " + this.getContent()) ;
            flag  = true ;    // 改变标志位,表示可以生产
            super.notify() ;
        }
        public void setName(String name){
            this.name = name ;
        }
        public void setContent(String content){
            this.content = content ;
        }
        public String getName(){
            return this.name ;
        }
        public String getContent(){
            return this.content ;
        }
    };
    class Producer implements Runnable{    // 通过Runnable实现多线程
        private Info info = null ;        // 保存Info引用
        public Producer(Info info){
            this.info = info ;
        }
        public void run(){
            boolean flag = false ;    // 定义标记位
            for(int i=0;i<50;i++){
                if(flag){
                    this.info.set("李兴华","JAVA讲师") ;    // 设置名称
                    flag = false ;
                }else{
                    this.info.set("mldn","www.mldnjava.cn") ;    // 设置名称
                    flag = true ;
                }
            }
        }
    };
    class Consumer implements Runnable{
        private Info info = null ;
        public Consumer(Info info){
            this.info = info ;
        }
        public void run(){
            for(int i=0;i<50;i++){
                this.info.get() ;
            }
        }
    };
    public class ThreadCaseDemo03{
        public static void main(String args[]){
            Info info = new Info();    // 实例化Info对象
            Producer pro = new Producer(info) ;    // 生产者
            Consumer con = new Consumer(info) ;    // 消费者
            new Thread(pro).start() ;
            new Thread(con).start() ;
        }
    };
  • 相关阅读:
    ABP 基于DDD的.NET开发框架 学习(四)时间控件采用datetimepicker注意事项
    解决Vs控制台程序出现NuGetprofile.ps1,因为在此系统上禁止运行脚本错误时或提示:“无法加载文件 .nugetpackagesMicrosoft.EntityFrameworkCore.Tools1.1.0-preview4-final oolsinit.ps1,因为在此系统上禁止运行脚本”
    git 本地初始化项目操作
    php(thinkphp)在linux系统下pdf转png图片【转】
    git学习笔记
    git 2.4.5编译安装
    mysql占用内存高的问题
    vmware下虚拟机centos,root登录时候提示鉴定故障解决方法
    SSH.net之主程序
    SSH.net之Service层
  • 原文地址:https://www.cnblogs.com/tszr/p/12152854.html
Copyright © 2011-2022 走看看