Java基础--多线程

关于多线程并发环境下,数据的安全问题

什么时候数据在多线程并发的环境下会存在安全问题
a.多线程并发
b.有共享数据
c.共享的数据有修改的行为

怎么解决线程安全问题
线程排队执行(不并发)==线程同步机制

模拟两个线程对同一份账户取款

1.不使用线程同步机制

public class Account {
    private int no;
    private double balance;

    public Account() {
    }

    public Account(int no, double balance) {
        this.no = no;
        this.balance = balance;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    public void withdraw(int n){
        double before=this.getBalance();
        double after=before-n;
        //模拟网络延迟
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.setBalance(after);
    }
}

public class AccountThread extends Thread{
    private Account act;
    public AccountThread(Account act){
        this.act=act;
    }
    public void run(){
        int money=5000;
        act.withdraw(money);
        System.out.println(Thread.currentThread().getName()+"取款成功"+act.getBalance());

    }
}

public class AccountTest {
    public static void main(String[] args) {
        Account act=new Account(001,10000);
        AccountThread T1=new AccountThread(act);
        T1.setName("t1");
        AccountThread T2=new AccountThread(act);
        T2.setName("t2");
        T1.start();
        T2.start();
    }
}

2.使用线程同步机制

public class Account {
    private int no;
    private double balance;

    public Account() {
    }

    public Account(int no, double balance) {
        this.no = no;
        this.balance = balance;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public double getBalance() {
        return balance;
    }

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

    public void withdraw(int n) {
        synchronized (this) {
            double before = this.getBalance();
            double after = before - n;
            //模拟网络延迟
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.setBalance(after);
        }
    }
}

public class AccountThread extends Thread{
    private Account act;
    public AccountThread(Account act){
        this.act=act;
    }
    public void run(){
        int money=5000;
        act.withdraw(money);
        System.out.println(Thread.currentThread().getName()+"取款成功"+act.getBalance());

    }
}

public class AccountTest {
    public static void main(String[] args) {
        Account act=new Account(001,10000);
        AccountThread T1=new AccountThread(act);
        T1.setName("t1");
        AccountThread T2=new AccountThread(act);
        T2.setName("t2");
        T1.start();
        T2.start();
    }
}

synchronized后年的小括号中传的数据必须是多线程共享的对象,才能达到多线程排队。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值