• java多线程之生产者消费者


    遇到问题,就要针对解决。多线程之生产者消费者实现。

    消费者

    public class Consumer implements Runnable {
    
        private Account account;
    
        public Consumer(Account accoun) {
    
            this.account = accoun;
    
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void run() {
    
            while (true) {
    
                synchronized (account) {
                    if (account.getAmount() >= 1) {
    
                        int balance = account.getAmount();
                        balance--;
                        account.setAmount(balance);
                        System.out.println(Thread.currentThread().getId() + "扣钱,"
                                + "余额: " + balance);
                        account.notify();
                    } else {
                        try {
                            System.out.println(Thread.currentThread().getId()
                                    + "钱不够: " + account.getAmount());
                            account.wait();
    
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                    }
    
                }
    
            }
    
        }
    
    }



    生产者

    public class Producer implements Runnable {
        private Account account;
    
        public Producer(Account accoun) {
    
            this.account = accoun;
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void run() {
    
            while (true) {
    
                synchronized (account) {
                    if (account.getAmount() <= 5000000) {
    
                        int balance = account.getAmount();
                        balance++;
                        account.setAmount(balance);
                        System.out.println(Thread.currentThread().getId() + "加钱,"
                                + "余额: " + balance);
                        account.notify();
                    } else {
                        try {
                            System.out.println(Thread.currentThread().getId()
                                    + "钱满了: " + account.getAmount());
                            account.wait();
    
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                    }
    
                }
    
            }
    
        }
    
    }
    测试类
    public class TestConPro {
    
        public static void main(String[] args) {
    
            Account account = new Account();
    
            Producer pro = new Producer(account);
            Consumer con = new Consumer(account);
    
            Thread tpro = new Thread(pro);
    
            Thread tcon = new Thread(con);
    
            tpro.start();
    
            tcon.start();
    
        }
    
    }
    
    
    
     
    原创博文,未经许可不得转载,转载请注明出处。
  • 相关阅读:
    Jmeter跨线程组传值
    python基础之高级函数
    Python基础之函数的应用
    python基础之文件的输入输出
    python基础练习之猜数字
    折腾了两天的跨站脚本提交问题,与IIS7有关
    python简介和环境搭建
    python paramiko
    Linux rsync 企业级应用
    Linux find 命令详解
  • 原文地址:https://www.cnblogs.com/Think-007/p/7777878.html
Copyright © 2020-2023  润新知