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

    
    

    synchronized(this){同步块}

    
    

    第一种处理方式  this 必须是唯一的   

    
    

    在后续的开发中,我们一般用这个唯一的类的字节码信息来表示唯一的。



    boolean getmoney(int m) throws InterruptedException { synchronized (Money.class){ if (m<
    =money){ Thread.sleep(1); money-=m; System.out.println(Thread.currentThread().getName()+"取钱成功,剩余"+money); return true; } } System.out.println(Thread.currentThread().getName()+"余额不足,剩余"+money); return false; }
    第二种
    synchronized   线程同步方法

    synchronized boolean getmoney(int m) throws InterruptedException { if (m<=money){ Thread.sleep(1); money-=m; System.out.println(Thread.currentThread().getName()+"取钱成功,剩余"+money); return true; } System.out.println(Thread.currentThread().getName()+"余额不足,剩余"+money); return false; }
  • 相关阅读:
    MongoDB 查询$关键字 $in $or $all
    MongoDB limit 选取 skip跳过 sort排序 方法
    POJ 1700
    POJ 1666
    POJ 1701
    POJ 1674
    POJ 1664
    POJ 1665
    POJ 1658
    POJ 1656
  • 原文地址:https://www.cnblogs.com/ww103/p/11754452.html
Copyright © 2011-2022 走看看