多线程处理获取大量QQ好友和微信好友

本文介绍了一种使用多线程和异步Future对象批量获取大量QQ和微信好友信息的方法。通过将数据分组处理并在多个线程中并行执行,能够在短时间内高效地收集到所需的好友数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:底层提供三个批量获取qq和微信的相关信息接口。都是通过dtcServiceFuture.calculate(List,Map)函数获取。由于业务场景规定在五秒内获取要求获取大量的QQ好友和微信好友数据信息,所有采用多线程方式获取数据。

dtcServiceFuture是一个异步调用Future对象

主要流程为:

第一阶段:采用Future对象将所有数据按照200为一组建立一个单线程,采用Future的好处在于,Futuer对象在建立期间,其他Future对象会在并行计算。

List<Future> friendsNetInfoFutureList = new ArrayList<Future>();
List<Future> friendsLevelInfoFutureList  = new ArrayList<Future>();
List<Future> friendsDpInfoFutureList = new ArrayList<Future>();

for(List<Integer> batchUserIds : compentList){
    Map<String, Object> contextMap = new HashMap<String, Object>();
    contextMap.put("userIds", batchUserIds);
    friendsNetInfoFutureList.add(getFuture(qq_wechat.get(type), contextMap));
    friendsLevelInfoFutureList.add(getFuture(serviceKey[0], contextMap));
    friendsDpInfoFutureList.add(getFuture(serviceKey[1], contextMap));
}
第二阶段:第一阶段建立的N个线程在不断并行执行,Future.get函数会等到所有future任务完全执行完毕后进行获取结果,采用Future的第二个好处在于可以获取返回结果。

List<HashMap<String,String>> friendsNetInfo = new ArrayList<HashMap<String, String>>();
HashMap<String, HashMap<String,String>> friendsLevelInfo = new HashMap<String, HashMap<String, String>>();
List<HashMap<String,String>> friendsDpInfo = new ArrayList<HashMap<String, String>>();

for(int i = 0; i < compentList.size();i++){
    Object temp;
    //根据用户id查询QQ或者微信号和昵称,qq号或者微信昵称可能会有多个
    temp = getInfo(qq_wechat.get(type), friendsNetInfoFutureList.get(i));
    if(temp != null){
        friendsNetInfo.addAll((List)temp);
    }
    //根据用户id获取星级
    temp = getInfo(serviceKey[0], friendsLevelInfoFutureList.get(i));
    if(temp != null){
        friendsLevelInfo.putAll((HashMap)temp);
    }
    // 根据用户查询用户昵称
    temp = getInfo(serviceKey[1], friendsDpInfoFutureList.get(i));
    if(temp != null){
        friendsDpInfo.addAll((List)temp);
    }
}

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值