@Autowired
StringRedisTemplate stringRedisTemplate;
public void handleJSON(JSONArray data, int threadNum){
int length = data.size();
int tl = length % threadNum == 0 ? length / threadNum : (length
/ threadNum + 1);
for (int i = 0; i < tl; i++) {
System.out.println("开了几个线程:"+i+"线程总数:"+tl);
Integer end = (i+1)*threadNum;
HandleThread thread = new HandleThread( data, i * threadNum, end > length ? length : end,type,supplierId,dbName);
thread.start();
}
// 开始redis 标识
String readGoods = UUID.randomUUID().toString();
Map<String, Object> value = new HashMap<String, Object>();
value.put("readGoods", "传入需要的判断标识");
String valueString = JSONObject.toJSONString(value);
stringRedisTemplate.opsForValue().set(readGoods, valueString, 45L, TimeUnit.MINUTES);
}
class HandleThread extends Thread {
private JSONArray data;
private int start;
private int end;
public HandleThread( JSONArray data, int start, int end) {
this.data = data;
this.start = start;
this.end = end;
}
public void run(){
//这里处理数据
JSONArray json = JSONArray.parseArray(JSON.toJSONString(data.subList(start, end)));
for (int i = 0; i < json.size(); i++) {
// 处理逻辑
}
}
}
public static void main(String[] args) throws Exception {
// 获取Redis校验
Response response = new Response();
if(redisGoods("Redis保存的标识") {
return response.failure("数据读取中,请过会操作。。。");
}
JSONArray json = JSON.parseArray(str);
handleJSON( json, 10000);
}
// Redis判断
public Boolean redisGoods(String dbName) {
String object = redisTemplate.opsForValue().get(dbName).toString;
if(object !=null && object.contains(dbName)) {
return true;
}
//Set tokenSet = stringRedisTemplate.keys("*");
// Iterator<String> iterator = tokenSet.iterator();
// JSONObject valueObject = null;
//while (iterator.hasNext()){
//try {
//String token = iterator.next();
//String value = stringRedisTemplate.opsForValue().get(token);
//valueObject = JSONObject.parseObject(value);
//if(valueObject !=null && valueObject.get("readGoods") !=null) {
//if(dbName.equals(valueObject.get("readGoods")) ) {
//return true;
//}
//}
//} catch (Exception e) {
// return false;
//}
//}
return false;
}
redis 面试 https://blog.csdn.net/qq_35190492/article/details/102841400