Callable线程

 

写一个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();
        }
    }

 

转载于:https://www.cnblogs.com/shihaiming/p/6703616.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值