zoukankan      html  css  js  c++  java
  • 同步 3

    package thread.syn2;
    
    import java.util.Arrays;
    
    public class Bank {
    
        private final double[] accounts;
        
        
        public Bank(int n,double initialBalance){
            accounts = new double[n];
            Arrays.fill(accounts, initialBalance); 
        }
        
        
        public synchronized void transfer(int from ,int to , double  amount){
            
            try {
                if (accounts[from]<amount) {
                    wait();
                }
                System.out.println(Thread.currentThread());
                accounts[from] -= amount;
                System.out.printf("%10.2f from %d to %d",amount,from,to); 
                accounts[to] += amount;
                System.out.printf("Toal Balance:%10.2f%n",getToalBalance()); 
                
                notifyAll();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
            }
        }
        
        
        public double getToalBalance(){
            double sum=0;
            for(double a: accounts){
                sum += a;
            }
            return sum;
        }
        
        public int size(){
            return accounts.length;
        }
        
    
    }
    package thread.syn2;
    
    public class TestSix {
    
        /**
         * 14.5.5 synchronized 关键字
         * 
         * 所可以执行代码片段,任何时候只有一个线程可以执行被保护的代码片段
         * 锁可以管理试图进入被保护的代码片段的线程
         * 锁可以有一个或多个条件对象
         * 每个条件对象管理那些已经进入被保护的代码片段但是还不能运行的线程
         * 
         * java 1开始每个对象都有一个内部锁
         * 
         * 如果一个方法用synchronized声明,对象的锁将保护整个方法。如果想调用这个方法,必须获得内部的对象锁。
         * 
         * 
         * 用synchronized代替显示的锁
         * 
         * 
         * 
         * wait() 等价于 await()
         * 
         * notifyAll() 等价于 signalAll()
         * 
         * 
         * 
         * 内部锁和条件对象有局限性
         * 不能中断一个视图获取内部锁的线程
         * 试图获取锁时,不能设定超时
         * 每个锁单一条件可能是不够的
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         */
        
        
        public static final int NACCOUTS = 100;
        public static final double INITIAL_BALANCE = 1000;
        public static final double MAX_AMOUNT  = 1000;
        public static final int DELAY = 10;
        
        public static void main(String[] args) {
            Bank bank = new Bank(NACCOUTS, INITIAL_BALANCE);
            for (int i = 0; i < NACCOUTS; i++) {
                int fromAccount = i;
                Runnable r = () -> {
                    try {
                        while(true){
                            int toAccount = (int) (bank.size() * Math.random());
                            double amount = MAX_AMOUNT * Math.random();
                            bank.transfer(fromAccount, toAccount, amount);
                            Thread.sleep((int) (DELAY*Math.random()));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
                Thread t = new Thread(r);
                t.start();
            }
        }
    
    }
  • 相关阅读:
    今天,我们来聊一聊互联网真的有你所期待的那么好吗?来自一个老码农的碎碎念
    新鲜出炉!阿里Java后端面经,已拿offer!
    面试阿里,字节跳动,美团必被问到的红黑树原来这么简单
    凭借着这份Spring面试题,我拿到了阿里,字节跳动美团的offer!
    深度分析:理解Java中的多态机制,一篇直接帮你掌握!
    gdb调试core dump使用
    665. Non-decreasing Array
    netstat命令详解
    ifconfig命令
    #paragma详解
  • 原文地址:https://www.cnblogs.com/lxh520/p/8467207.html
Copyright © 2011-2022 走看看