zoukankan      html  css  js  c++  java
  • 高并发下减少锁竞争

    1.减少锁的持有时间,将不需要锁的操作从同步代码块的移除。

    1. //可以优化的代码  
    2. class AttributeStore{  
    3.     private final Map<String,String> attributes=new HashMap<String,String>();  
    4.     public synchronized boolean userLocationMatches(String username,String regex){  
    5.         String key="user."+username;  
    6.         String location=attributes.get(key);  
    7.         if(location==null)  
    8.             return false;  
    9.         else  
    10.             return Pattern.matches(regex,location);  
    11.     }  
    12. }  
    1. //优化之后的代码  
    2. class AttributeStore{  
    3.     private final Map<String,String> attributes=new HashMap<String,String>();  
    4.     public boolean userLocationMatches(String username,String regex){  
    5.         String key="user."+username;  
    6.         String location;  
    7.         synchronized (this) {  
    8.             location=attributes.get(key);             
    9.         }  
    10.         if(location==null)  
    11.             return false;  
    12.         else  
    13.             return Pattern.matches(regex,location);  
    14.     }  
    15. }  

    2.降低锁的粒度

    1. //可以锁分解的代码  
    2. class ServerStatus{  
    3.     private  Set<String> users;  
    4.     private  Set<String> queries;  
    5.     public synchronized void addUser(String user){  
    6.         users.add(user);  
    7.     }  
    8.     public synchronized void removeUser(String user){  
    9.         users.remove(user);  
    10.     }  
    11.       
    12.     public synchronized void addQuery(String query){  
    13.         queries.add(query);  
    14.     }  
    15.     public synchronized void removeQuery(String query){  
    16.         queries.remove(query);  
    17.     }     
    18. }  
    1. //优化后的代码  
    2. class ServerStatus{  
    3.     private  Set<String> users;  
    4.     private  Set<String> queries;  
    5.     public  void addUser(String user){  
    6.         synchronized (users) {  
    7.             users.add(user);  
    8.         }  
    9.     }  
    10.     public  void removeUser(String user){  
    11.         synchronized (users) {  
    12.             users.remove(user);  
    13.         }  
    14.     }  
    15.       
    16.     public  void addQuery(String query){  
    17.         synchronized (queries) {  
    18.             queries.add(query);  
    19.         }  
    20.     }  
    21.     public  void removeQuery(String query){  
    22.         synchronized (queries) {  
    23.             queries.remove(query);  
    24.         }  
    25.     }     
    26. }  
  • 相关阅读:
    uboot和内核分区的修改
    2440移植内核到uboot上,打印乱码
    启动新内核出现:No filesystem could mount root, tried: ext3 ext2 cramfs vfa
    启动新内核出现:Kernel panic
    移植最新版本3.4.2内核
    2017团体程序设计天梯赛大区赛 L3-3 球队“食物链”
    leetcode543 Diameter of Binary Tree
    CF599B Spongebob and Joke
    poj1930 Dead Fraction
    poj3040 Allowance
  • 原文地址:https://www.cnblogs.com/minshia/p/7045045.html
Copyright © 2011-2022 走看看