多线程wait等待和notify唤醒

package com.ibeidou.thread;
/**
 * 测试wait和notify方法
 * 两个线程操作同一对象,一个线程调用wait方法,另一个之后调用notify方法
 * 等待是当前线程对对象等待,唤醒是线程对等待对象的所有线程唤醒(可随机唤醒一个,可唤醒所有的)
 * @author sam 2012-4-11
 */
public class TestWaitAndNotify {
        Account account = new Account("Boy",100d);
        class Account{
                String name;
                double balance;
                Account(String name, double balance){
                        this.name = name;
                        this.balance = balance;
                }
                void deposit(double money){
                        System.out.println("Before deposit balance is " + balance);
                        this.balance += money;
                        System.out.println("After deposit balance is " + balance);
                }
                void withdraw(double money){
                        System.out.println("Before withdraw balance is " + balance);
                        this.balance -= money;
                        System.out.println("After withdraw balance is " + balance);
                }
        }

        class BoyThread extends Thread{
                @Override
                public void run(){
                        for(int i = 0; i < 10; i++){
                                synchronized (account){
                                        account.deposit(10d);
                                        //随机唤醒一个等待account的线程
                                        account.notify();
                                        //唤醒所有等待account的线程
                                        //account.notifyAll();
                                }
                        }
                }
        }
        class GirlThread extends Thread{
                @Override
                public void run(){
                        try{
                                for(int i = 0; i < 10; i++){
                                        synchronized (account){
                                                //总是需要判断余额,因为当线程被唤醒时,有可能余额不足0
                                                while(account.balance <= 0){

                                                        //当前线程开始等待account对象
                                                        account.wait();
                                                }
                                                account.withdraw(50d);
                                        }
                                }
                        }catch(Exception e){
                                e.printStackTrace();
                        }
                }
        }
        void testWaitAndNotify(){
                BoyThread boy = new BoyThread();
                GirlThread girl = new GirlThread();
                boy.start();
                girl.start();
        }
        public static void main(String[] args){
                TestWaitAndNotify twan = new TestWaitAndNotify();
                twan.testWaitAndNotify();
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值