@PostMapping("/traffic/records")
public List listTrafficRecord() {
List doorRecordInfoVOList = doorApiService.doorRecordList(null);
return doorRecordInfoVOList.stream()
.limit(3)
.collect(Collectors.toList());
}
@GetMapping("/visitor/record")
public List listVisitorRecord() {
List doorRecordInfoVOList = doorApiService.doorRecordList("4");
return doorRecordInfoVOList.stream()
.limit(2)
.collect(Collectors.toList());
}
service 实现
@Override
public List doorRecordList(String typeFlag) {
reentrantLock.lock();
try {
//首先获取 token
String token = this.getToken();
if (StrUtil.isEmpty(token)) {
return null;
}
List doorRecordInfoVOList = new ArrayList();
LocalDateTime now = LocalDateTime.now();
LocalDateTime start = now.minusHours(12L);
return handleList(doorRecordInfoVOList, now, start, token, typeFlag, 0);
}finally {
reentrantLock.unlock();
}
}
private List handleList(List doorRecordInfoVOList,
LocalDateTime now,
LocalDateTime start,
String token,
String typeFlag,
int retryCount) {
//判断两个时间是不是在同一天
if (now.toLocalDate().equals(start.toLocalDate())) {
//在同一天组装时间范围
String date = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String startTime = start.format(DateTimeFormatter.ofPattern("HHmmss"));
String endTime = now.format(DateTimeFormatter.ofPattern("HHmmss"));
JSONArray recordArray = this.apiResult(token, date, startTime, endTime);
this.addArrayToList(recordArray, doorRecordInfoVOList, typeFlag);
} else {
//不在同一天
String date = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String endTime = now.format(DateTimeFormatter.ofPattern("HHmmss"));
JSONArray recordArray1 = this.apiResult(token, date, "000000", endTime);
this.addArrayToList(recordArray1, doorRecordInfoVOList, typeFlag);
//加上前一天时间段
String preDate = start.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String startTime = start.format(DateTimeFormatter.ofPattern("HHmmss"));
JSONArray recordArray2 = this.apiResult(token, preDate, startTime, "235959");
this.addArrayToList(recordArray2, doorRecordInfoVOList, typeFlag);
}
if (doorRecordInfoVOList.size() doorRecordInfoVOList, String typeFlag) {
if (ObjectUtil.isEmpty(array)) return;
for (int i = 0; i requestMap = new HashMap();
Map map = new HashMap();
map.put("beginTime", startTime);
map.put("endTime", endTime);
map.put("date", date);
requestMap.put("data", map);
//调用 api 获取数据
String body = HttpRequest
.post(getDoorRecordInfoUrl)
.header("token", token)
.header("appId", appId)
.header("Content-Type", "application/json")
.body(JSONUtil.toJsonStr(requestMap))
.execute()
.body();
JSONObject resultJson = JSONUtil.parseObj(body);
Integer code = resultJson.getInt("code");
if (code == 200) {
JSONObject data = resultJson.getJSONObject("data");
jsonArray = data.getJSONArray("data");
}
return jsonArray;
}
上述实现如果我不加上 reentrantLock ,doorRecordInfoVOList 线程安全问题,有一个接口直接返回空了,但是直接分开调用,都有返回,请问这个为啥会有线程安全问题,求大佬指教