zoukankan      html  css  js  c++  java
  • Java线程同步

    public class Accout {
    
        private static Account account = new Account();
        
        public static void main(String[] args) {
            ExecutorService executor = Executors.newCachedThreadPool();
    
            for (int i = 0; i < 1000; i++) {
                executor.execute(new AddAPennyTask());
            }
            executor.shutdown();
            
            while (!executor.isTerminated()){
                
            }
            System.out.println("账户余额:" + account.getBalance());
        }
        
        private static class Account{
            private int balance = 0;
            
            public int getBalance(){
                return balance;
            }
            
            public void deposit(int amount){
                balance = balance + amount;
                try {
                    Thread.sleep(10);
                }catch (Exception e ){
                    
                }
            }
        }
        
        private static class AddAPennyTask implements Runnable{
    
            @Override
            public void run() {
                account.deposit(1);
            }
        }
    }
  • 相关阅读:
    AC自动机学习笔记(模板)
    codeforces1328E
    Codeforces 1288E- Messenger Simulator (树状数组)
    线性基小记
    HDU3949
    矩阵快速幂小记
    5E
    5D
    5C
    5B
  • 原文地址:https://www.cnblogs.com/masterhxh/p/13921982.html
Copyright © 2011-2022 走看看