java同步线程(二)

java同步线程(二)

           同步方法

同步方法:

使用synchronized关键字来修饰某个方法,则该方法称为同步方法。
同步方法的同步监视器是this,也就是该对象本身。

通过同步方法可以非常方便地实现线程安全的类,线程安全的类具有如下特征。

1、该类的对象可以被多个线程安全地访问。

2、每个线程调用该对象的任意方法之后都将得到正确结果。

3、每个线程调用该对象的任意方法之后,该对象状态依然保持合理的状态。

实现方式如下:

package com.zmy.bank;

public class Account {
    //封装账户编号,账户余额两个Eield
    private String accountNo;
    private double balance;
    public Account(String accountNo,double balance)
    {
        this.accountNo=accountNo;
        this.balance=balance;
    }
    //创建的getter和setter方法

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }


    public synchronized void draw(double drawAmount)
    {
        if(balance>=drawAmount)
        {
            System.out.println(Thread.currentThread().getName()+"取钱成功!吐出钞票"+drawAmount);
            try{
                Thread.sleep(1);

            }catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            balance-=drawAmount;
            System.out.println("\t余额为:"+balance);
        }
        else {
            System.out.println(Thread.currentThread().getName()+"取钱失败!余额不足!");
        }
    }
    @Override
    public int hashCode() {
        return accountNo.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this==obj)
            return true;
        if (obj !=null&&obj.getClass()==Account.class)
        {
            Account target=(Account)obj;
            return target.getAccountNo().equals(accountNo);
        }
        return false;
    }


}
package com.zmy.bank;

public class DrawThread extends Thread {
    private Account account;
    private double drawAmount;

    public DrawThread(String name, Account account, double drawAmount) {
        super(name);
        this.account = account;
        this.drawAmount = drawAmount;
    }
    //当多个线程修改同一个共享数据时,将涉及数据安全性问题

    @Override
    public void run() {
        if (account.getBalance() >= drawAmount) {
            //突出钞票
            account.draw(drawAmount);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } else {
            System.out.println(getName() + "取钱失败!余额不足!");
        }
    }
}

package com.zmy.bank;

public class DrawTest {
    public static void main(String[] args) {
        //创建一个账户
        Account acct=new Account("123456",1000);
        //simulation two thread draw money from one account
        new DrawThread("甲",acct,800).start();
        new DrawThread("乙",acct,800).start();
    }
}

synchronized 不能修饰构造器、属性

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值