线程同步的不安全案例与解决办法

线程同步的不安全案例与解决办法,第1张

线程同步的不安全案例与解决办法 正文:

1 首先是车站卖票的不安全例子:

package 多线程;

public class 线程同步 {
    public static void main(String[] args) {
        BuyTikcet station=new BuyTikcet();

        new Thread(station,"小明").start();
        new Thread(station,"小红").start();
        new Thread(station,"小小").start();

    }
}


class BuyTikcet implements Runnable{
    private int ticketNums=10;
    boolean flag=true;
    @Override
    public void run() {
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    private void buy() throws InterruptedException {
        if(ticketNums<=0){
            flag=false;
            return;
        }
        Thread.sleep(700);
        System.out.println(Thread.currentThread().getName()+ticketNums--);
    }
}


因为就一个对象 (Buyticket) 所以多个线程访问这同一个对象 会出现线程不安全的情况 这是把方法加上锁就可以了

2 银行取款的不安全实例

package 多线程;

public class Bank {

    public static void main(String[] args) {
        Account account=new Account(100,"结婚基金");
        Drawing you=new Drawing(account,50,"you");
        Drawing girl=new Drawing(account,100,"girl");
        you.start();
        girl.start();
    }
}
class Account{
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}


//不涉及多个线程 *** 作同一个对象 因为每个取款都是一个对象
//但是在买票那里 多个线程 *** 作同一个对象
class Drawing extends Thread{
    Account account;
    int drawingMoney;
    int nowMoney;


    public Drawing(Account account,int drawingMoney,String name){
        super(name);
        this.account=account;
        this.drawingMoney=drawingMoney;
    }

    @Override
    public void run() {
        //synchronized (account){
            if(account.money-drawingMoney<0){
                System.out.println(Thread.currentThread().getName()+"钱不够了");
                return;
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.money=account.money-drawingMoney;
            nowMoney=nowMoney+drawingMoney;
            System.out.println(account.name+"剩下的钱"+account.money+this.getName());
            System.out.println(this.getName()+"手里的钱"+nowMoney);
       // }


    }
}

可以看到如果还是对run方法加锁的话 是没有用的,因为这是多个对象 而不是一个对象,多个对象 碰到方法锁时,他会发现也没有别的线程来抢占这个对象,所以其实这两个对象互不干扰 属于两个空间。
真正应该加锁的是account公共资源,所以要使用代码块。即上文注释掉的地方

3 不安全集合

package 多线程;

import java.util.ArrayList;
import java.util.List;

public class arraylist集合 {

    public static void main(String[] args) throws InterruptedException {
        List list=new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            synchronized (list){
                new Thread(()->{
                    list.add(Thread.currentThread().getName());

                }).start();

            }

        }
        Thread.sleep(1000);
        System.out.println(list.size());
    }
}

在这里加一个睡眠,是为了更好的发现问题,有可能for循环没跑完 就跑到下面打印了,然后这里保证安全也是代码块锁住公共资源list

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5681088.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存