//极验注册分值>=90,跳到错误页面,提示语:该手机号暂时无法在线注册,请联系客服。
String mobile = request.getMobile();long rainScore = getRainScores(mobile);
。。。。。。。。。。。。。。。。
public boolean tryLock(String lockKey) {
return tryLock(lockKey, DBConfig.DEFAULT_LOCK_SECCONDS, DBConfig.DEFAULT_TRYLOCK_TIMEOUT_SECONDS);
}
public boolean tryLock(String lockKey, int lockSec, int timeOutSec) {
MemCachedClient client = getMemCachedClient(false);
long start = System.currentTimeMillis();
while (true) {
boolean locked = client.add(lockKey, "", getDateAfter(lockSec));
if (locked) {
return true;
} else {
long now = System.currentTimeMillis();
long costed = now - start;
if (costed >= timeOutSec * 1000) {
return false;
}
}
}
}
/**
* 该锁是否已被持有 false:未被持有,true:已被持有
*
* @param lockKey
* @param lockSec
* @return
*/
public boolean isHoldLock(String lockKey, int lockSec) {
boolean isHoldLock = false;
MemCachedClient client = getMemCachedClient(false);
boolean locked = client.add(lockKey, "holded", getDateAfter(lockSec));
if (!locked) {
isHoldLock = true;
}
return isHoldLock;
}
public boolean releaseLock(String lockKey) {
MemCachedClient client = getMemCachedClient(false);
return client.delete(lockKey);
}
}
Java中的读写锁:
多个读锁不互斥, 读锁与写锁互斥, 写锁与写锁互斥, 这是由JVM自行控制的,我们只要上好相应的锁即可。
package
com.cn.gbx;
import
java.util.HashMap;
import
java.util.Map;
import
java.util.concurrent.locks.ReadWriteLock;
import
java.util.concurrent.locks.ReentrantReadWriteLock;
public
class
CacheDesign {
private
Map cache =
new
HashMap();
//对象锁的设计
// public synchronized Object getData(String key){
// Object value = null;
// value = cache.get(key);
// if (value == null) {
// value = "queryDao";
// cache.put(key, value);
// }
// return value;
// }
//可重入锁的设计
static
ReadWriteLock rwl =
new
ReentrantReadWriteLock();
public
synchronized
Object getData(String key){
Object value =
null
;
rwl.readLock().lock();
try
{
value = cache.get(key);
if
(value ==
null
) {
rwl.readLock().unlock();
rwl.writeLock().lock();
try
{
if
(value ==
null
) {
//确保后边的线程不会重读写
value =
"queryDao"
;
cache.put(key, value);
}
}
finally
{
rwl.writeLock().unlock();
}
rwl.readLock().lock();
}
}
finally
{
rwl.readLock().unlock();
}
return
value;
}
public
static
void
main(String[] args) {
}
}