三大线程不安全案例

三大不安全案例

  • 买票问题

    //买票
    public class Demo01 {
        public static void main(String[] args) {
            BuyTickets buyTickets = new BuyTickets();
            new Thread(buyTickets,"person1").start();
            new Thread(buyTickets,"person2").start();
            new Thread(buyTickets,"person3").start();
        }
    }
    class BuyTickets implements Runnable{
        private int ticketNum = 20;
        boolean flag = true;
        @Override
        public void run() {
            while (flag){
                try {
                    buy();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        private void buy() throws Exception {
            //判断是否有票
            if (ticketNum < 1){
                flag = false;
                return;
            }
            //模拟延时
            Thread.sleep(100);
            //买票
            System.out.println(Thread.currentThread().getName()+"第"+ticketNum--+"张票");
        }
    }
    //person2第10张票
    person3第9张票
    person1第8张票
    person2第7张票
    person1第6张票
    person3第6张票
    person1第4张票
    person3第3张票
    person2第5张票
    person3第2张票
    person1第1张票
    person2第0张票
    person3第-1张票
    
  • 不安全取款

    //不安全取款
    public class Demo02 {
        public static void main(String[] args) {
            //账户
            Account account = new Account(100,"A");
            Bank person1 = new Bank(account, 20, "Person1");
            Bank person2 = new Bank(account, 90, "person2");
            person1.start();
            person2.start();
        }
    }
    //账户
    class Account{
        int money;
        String name;
    
        public Account(int money, String name) {
            this.money = money;
            this.name = name;
        }
    }
    //银行
    class Bank extends Thread{
        Account account;
        int outMoney;
        int nowMoney;
        public Bank(Account account,int outMoney,String name){
            super(name);
            this.outMoney = outMoney;
            this.account = account;
        }
    
        @Override
        public void run() {
            //判断是否还有钱
            if(account.money-outMoney<0){
                System.out.println(Thread.currentThread().getName()+"钱不够了");
                return;
            }
            //sleep可以放大问题的发生性
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            //卡内余额
            account.money = account.money - outMoney;
            //手里的钱
            nowMoney = nowMoney +outMoney;
            System.out.println(account.name+"余额为:"+account.money);
            //Thread.currentThread().getName() =  this.getName()
            System.out.println(this.getName()+"手里的钱"+nowMoney);
        }
    //A余额为:-10
    Person1手里的钱20
    A余额为:-10
    person2手里的钱90    
    
  • 线程不安全集合

    //线程不安全集合
    public class Demo03 {
        public static void main(String[] args) throws InterruptedException {
            final ArrayList<String> arrayList = new ArrayList<String>();
            for (int i = 0; i < 10000; i++) {
                new Thread(()->{
                    arrayList.add(Thread.currentThread().getName());
                }).start();
            }
            Thread.sleep(3000);
            System.out.println(arrayList.size());
        }
    }
    
    //9984
    
posted @ 2021-04-25 14:47  saxon宋  阅读(65)  评论(0)    收藏  举报