• 三大不安全案例


    买票

    /**
     * 不安全买票测试
     */
    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());
        }
    }
    
  • 相关阅读:
    区别是否是 微信浏览器 ?
    添加收藏
    函数调用那点事
    移动端返回上一页,刚需!document.referrer 详解
    蓝桥杯-乘积尾零
    printf不能直接输出string类型
    bitset的简单用法
    to_string()函数(C++)
    蓝桥杯-明码
    手写C语言字符库
  • 原文地址:https://www.cnblogs.com/gbhh/p/13768095.html
Copyright © 2020-2023  润新知