zoukankan      html  css  js  c++  java
  • 三大不安全案例

    买票

    /**
     * 不安全买票测试
     */
    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());
        }
    }
    
  • 相关阅读:
    Java偏向锁浅析
    Activity 之 Launch Mode
    ActionBarSherlock & ViewPagerIndicator
    JNI 和 NDK 介绍
    Android 应用缓存技术提高程序性能
    设计模式之抽象工厂模式
    设计模式之工厂方法模式
    Android 2.3 AsyncQueryHandler Cursor内存泄漏问题
    ubuntu系统右键菜单添加【当前路径打开终端】选项
    设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/gbhh/p/13768095.html
Copyright © 2011-2022 走看看