81,092
社区成员
发帖
与我相关
我的任务
分享
public class MainClass {
public static void main(String[] args) throws InterruptedException {
for(int i = 0;i<100;i++){
Thread t = new Thread(new TaskRunnable(i)); //启动一百个线程测试
t.start();
}
}
}
public class TaskRunnable implements Runnable{
private int operationNumber;
public TaskRunnable(int a){
this.operationNumber = a;
}
@Override
public void run() {
Action action = new Action();
Bean bean = action.getAndUpdateBeanFromCache();
System.out.println("threadId is id = " + Thread.currentThread().getId());
if(bean==null){
bean = new Bean();
bean.setUserId("12344");
bean.setCount(11);
}
Map<String,String> map = bean.getMap();
map.put(operationNumber+"",operationNumber+"");
System.out.println("map key = " + operationNumber + " ," + " value = " + operationNumber);
RedisUtil.setCache(RedisConstant.testKey, new Gson().toJson(bean));
}
}
public class Action {
public Bean getAndUpdateBeanFromCache(){
Bean bean = new Bean();
String key = RedisConstant.testKey;
String value = RedisUtil.getCache(key);
Type type = new TypeToken<Bean>(){}.getType();
bean = new Gson().fromJson(value,type);
return bean;
}
}