机器人模块-优化死循环、优化多余的锁

pull/1/head
luzhaofeng 5 years ago
parent 4440d0cc15
commit 411d06c94b
  1. 34
      blockchain-server/blockchain-server-databot/src/main/java/com/blockchain/server/databot/schedule/KMarketScheduling.java

@ -43,7 +43,7 @@ public class KMarketScheduling {
private static final Logger LOG = LoggerFactory.getLogger(KMarketScheduling.class); private static final Logger LOG = LoggerFactory.getLogger(KMarketScheduling.class);
private static final int DEPTH_TOTAL_AMOUNT = 150; //盘口数据总量 private static final int DEPTH_TOTAL_AMOUNT = 150; //盘口数据总量
private static ExecutorService cachedThreadPool = Executors.newFixedThreadPool(100); //异步线程池 private static ExecutorService cachedThreadPool = Executors.newFixedThreadPool(4); //异步线程池
/*** /***
* 每一秒创建一次K线数据和深度数据 * 每一秒创建一次K线数据和深度数据
@ -332,15 +332,14 @@ public class KMarketScheduling {
} }
//格式化数据 //格式化数据
float unitPrice = formatValue(depthPrice); float unitPrice = formatValue(depthPrice);
//判断是否存在相同单价
if (newPrices.containsKey(unitPrice)) {
continue;
} else {
//新创盘口数量 //新创盘口数量
float newAmount = avgAmount * random.nextInt(100) / 50; float newAmount = avgAmount * random.nextInt(100) / 50;
//减去发行总数量 //减去发行总数量
totalAmount -= newAmount; totalAmount -= newAmount;
//判断是否存在相同单价
if (newPrices.containsKey(unitPrice)) {
continue;
} else {
//构建盘口数据对象 //构建盘口数据对象
MarketDTO marketDTO = new MarketDTO(new BigDecimal(unitPrice + ""), new BigDecimal(newAmount + ""), MarketDTO marketDTO = new MarketDTO(new BigDecimal(unitPrice + ""), new BigDecimal(newAmount + ""),
new BigDecimal(newAmount + ""), unitName, coinName, tradingType); new BigDecimal(newAmount + ""), unitName, coinName, tradingType);
@ -377,20 +376,10 @@ public class KMarketScheduling {
tuples.add(tuple); tuples.add(tuple);
} }
boolean lockFlag = false;
//获取锁并更新缓存
while (!lockFlag) {
lockFlag = orderCache.tryFairLock(coinName, unitName, tradingType);
//获取锁成功
if (lockFlag) {
//删除集合中所有数据 //删除集合中所有数据
orderCache.removeZSetByRange(coinName, unitName, tradingType, 0, -1); orderCache.removeZSetByRange(coinName, unitName, tradingType, 0, -1);
//将新数据设置进缓存中 //将新数据设置进缓存中
orderCache.setZSetValue(coinName, unitName, tradingType, tuples); orderCache.setZSetValue(coinName, unitName, tradingType, tuples);
//释放锁
orderCache.unFairLock(coinName, unitName, tradingType);
}
}
} }
/*** /***
@ -524,21 +513,8 @@ public class KMarketScheduling {
* @param totalAmount * @param totalAmount
*/ */
private void setUpOneDayKTotalAmount(String currencyPair, BigDecimal totalAmount) { private void setUpOneDayKTotalAmount(String currencyPair, BigDecimal totalAmount) {
//锁key
String lockKey = currencyConfigCache.getCurrencyKDayAmountLockKey(currencyPair);
//锁标识
boolean lockFlag = false;
//循环获取锁
while (!lockFlag) {
lockFlag = currencyConfigCache.tryFairLock(lockKey);
//获取成功
if (lockFlag) {
//创建每天每天深度数量,设置进缓存中 //创建每天每天深度数量,设置进缓存中
createOneDayKTotalAmount(currencyPair, totalAmount); createOneDayKTotalAmount(currencyPair, totalAmount);
//释放锁
currencyConfigCache.unFairLock(lockKey);
}
}
} }
/*** /***

Loading…
Cancel
Save