写一个Callable线程
import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; public class SaveGameCallable implements Callable<HashMap<String, Object>> { private List<Phone> phoneList; private HashMap<String, Object> data; public List<Phone> getPhoneList() { return phoneList; } public void setPhoneList(List<Phone> phoneList) { this.phoneList = phoneList; } public SaveGameCallable(List<Phone> phoneList, HashMap<String, Object> data) { super(); this.phoneList = phoneList; this.data = data; } @Override public HashMap<String, Object> call() throws Exception { List<Map<String, Object>> userGameList = new LinkedList<Map<String, Object>>(); int size = phoneList.size(); UserService userService = (UserService) BeanUtil.getBean("userService"); for (int j = 0; j < size; j++) { Thread.sleep(50); Phone phonexx = phoneList.get(j); String mobilephone = phonexx.getMobilePhone(); if(StringUtil.isBlank(mobilephone)){ continue; } User user = userService.getUserByPhone(mobilephone); if(user != null && !user.equals("")){ Map<String, Object> map = new HashMap<String, Object>(20); map.put("success","true"); if (StringUtil.isNotBlank(user.getCardId())) { //前六位置为*,7-14与17位显示 ,其他都为* map.put("idnumber", "******" + user.getCardId().substring(6, 14) + "**" + user.getCardId().substring(16, 17) + "*"); } else { map.put("idnumber", "");//前六位置为*,7-14与17位显示 ,其他都为* } userGameList.add(map); data.put("userGameList", userGameList); }else{ data.put("errorCode", "该手机号【"+mobilephone+"】用户不存在!"); } } return data; } }
调用Callable线程:
public void getBorrowTenderLog() { try{ //调用线程处理 ExecutorService exec = Executors.newCachedThreadPool(); try { Future<HashMap<String, Object>> result = exec.submit(new BorrowTenderLogCallable(phoneModel.getPhone(), (HashMap<String, Object>) data)); data = result.get(); } catch (Exception e1) { e1.printStackTrace(); } finally { exec.shutdown(); } LOGGER.info("【获取日志信息: 】"+getStringOfJpaObj(data)); printWebJson(getStringOfJpaObj(data)); data.clear(); //优化JVM年轻代资源释放速度 data = null; } catch (Exception e) { LOGGER.error(e.getMessage()); e.printStackTrace(); } }