java多线程之生产者消费者

本文介绍了一种使用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();

    }

}

 

 

转载于:https://www.cnblogs.com/Think-007/p/7777878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值