ReentrantLock

来源:互联网 发布:南昌大学软件学院吧 编辑:程序博客网 时间:2024/06/03 19:31
/*     */   private Lock getLock(String lockKey) {/* 167 */     Lock l = (Lock)this.locks.get(lockKey);/* 168 */     if (l == null) {/* 169 */       synchronized (this.lock) {/* 170 */         l = (Lock)this.locks.get(lockKey);/* 171 */         if (l == null) {/* 172 */           l = new ReentrantLock();/* 173 */           this.locks.put(lockKey, l);/*     */         }/*     */       }/*     */     }/* 177 */     return l;/*     */   }


获取锁代码片段。


/*     */   private String nextOid(String sid)/*     */   {/* 102 */     if (StringUtils.isBlank(sid)) {/* 103 */       sid = "uapcloud";/*     */     }/*     */     /* 106 */     String key = sid;/* 107 */     if (StringUtils.isBlank(key)) {/* 108 */       throw new RuntimeException("schema code can not be null! please check global conf and context info!");/*     */     }/*     */     /*     */ /* 112 */     Lock l = getLock(key);/*     */     /* 114 */     OidCounter oidCounter = null;/* 115 */     String oidBase = null;/* 116 */     String nextOid = null;/*     */     /*     */     try/*     */     {/* 120 */       l.lock();/*     */       /* 122 */       oidCounter = (OidCounter)oidMap.get(key);/* 123 */       if (oidCounter == null) {/* 124 */         oidCounter = new OidCounter();/* 125 */         oidMap.put(key, oidCounter);/*     */       }/*     */       /* 128 */       if ((oidCounter.amount % OID_AMOUNT == 0) || (0 == oidCounter.amount)) {/* 129 */         oidBase = getNewBaseId(key);/*     */       } else {/* 131 */         oidBase = oidCounter.oidBase;/*     */       }/*     */       /* 134 */       nextOid = UapOidAlgorithm.getInstance(oidBase).nextOidBase();/* 135 */       oidCounter.oidBase = nextOid;/* 136 */       oidCounter.amount += 1;/*     */     } catch (Exception e) {/* 138 */       LOGGER.error("get oid error!", e);/* 139 */       throw e;/*     */     }/*     */     finally {/* 142 */       l.unlock();/*     */     }/*     */     /* 145 */     if (nextOid == null) {/* 146 */       return null;/*     */     }/*     */     /* 149 */     return getWholeOid(sid, nextOid);/*     */   }


Lock使用代码示例。

原创粉丝点击