java同步锁

 public   class   DataCache   {   
          protected   static   final   HashMap   map   =   new   HashMap();   //   Cache   table   
    
          private   static   final   Object   lock   =   new   Object();   
          private   DataCache()   {}   //   防止在外部实例化   
          public   static   Object   getData(Object   key)   {   
                  Object   v   =   map.get(key);   
                  if   (v   ==   null)   {   
                        synchronized(lock)   {     
                        v   =   map.get(key);     //   Check   again   to   avoid   re-load   
                        if   (v   ==   null)   loadDataSource(key);     
                        v   =   map.get(key);   //   retrieves   data.   
                        }   
    
                  }   
    
                  return   v;   
                    
          }   
          /*   
            *Load   data   from   data   source.   
            */   
          protected   static   synchronized   void   loadDataSource(Object   key)   {   
                  Object   value   =   new   Object();   //   Load   value   from   data   source   
                  map.put(key,   value);   
          }   
  }
 

你可能感兴趣的:(java)