首页 > 其他 > 详细

三大不安全案例

时间:2020-10-04 20:20:49      阅读:96      评论:0      收藏:0      [点我收藏+]

买票

/**
 * 不安全买票测试
 */
public class NotSafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        Thread t1 = new Thread(buyTicket,"嘟嘟");
        Thread t2 = new Thread(buyTicket,"肝肝");
        Thread t3 = new Thread(buyTicket,"钢镚");
        Thread t4 = new Thread(buyTicket,"核恒");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class BuyTicket implements Runnable{
    private int ticket = 100;
    private boolean flag = true;
    @Override
    public void run() {
        while (flag){
            buy();
        }
    }
    // synchronized 同步锁
    public synchronized void buy(){
        if(ticket <= 0){
            flag = false;
            return ;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"买到了票 :"+ticket--);
    }

银行取钱

/**
 * 不安全银行取钱测试
 */
public class NotSafeBank {
    public static void main(String[] args) {
        Account account = new Account(100,"结婚基金");
        Bank g = new Bank(account, 50, "肝肝");
        Bank d = new Bank(account, 100, "嘟嘟");

        new Thread(g).start();
        new Thread(d).start();

    }
}

class Account{
    int money;
    String name;

    public Account(int money,String name) {
        this.money = money;
        this.name = name;
    }
}
class Bank implements Runnable{
    private int out;
    private int nowMoney;
    private String name;
    private Account account;

    public Bank(Account account,int out, String name) {
        this.out = out;
        this.account = account;
        this.name = name;
    }

    @Override
    public void run() {
        //同步块
        synchronized (account){
            account.money = account.money - out;
            if(account.money < 0 ){
                System.out.println(account.name +"钱不够了 取不了!");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            nowMoney = out;
            System.out.println(this.name +"取到了"+nowMoney);
            System.out.println(account.name+" 余额为:"+account.money);
        }
    }
}

集合

/**
 * 不安全集合
 */
public class NotSafeList {
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            new Thread(()-> {
                synchronized (list){

                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        Thread.sleep(2000);
        System.out.println(list.size());
    }
}

三大不安全案例

原文:https://www.cnblogs.com/gbhh/p/13768095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!