Package cputils

Source Code of cputils.TBDataThread

package cputils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

import jsoup.ConnectionManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import common.Utils;

public class CPHMTBService extends AbstractLinkAndData{
  private static final Logger logger = Logger.getLogger(CPHMTBService.class);
 
  public static String[] popusers = {"他郎酒馆","bensen0612","四海数码2003","毛毛weiqing","shuxin680","zml88623535",
    "永不放弃3393","有啊旗航店","nbcx222222","宁波天一阁","youcai1978","songsonghehehehe"};
 
//  private static DateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
 
  //--合买红人 lotteryTypeId=1双色球   lotteryTypeId=8大乐透
  //http://caipiao.taobao.com/lottery/ajax/get_united_reds.htm?lotteryTypeId=8
 
  //--近期合买中奖
  //http://caipiao.taobao.com//lottery/ajax/last_issue_award_rank_list.htm?type=1&page=1&issue_id=X2R6VZ42TGEEISLV7AQP6NU6VI&lotteryTypeId=1&d=20140426

  //--合买大奖排行
  //http://caipiao.taobao.com/lottery/ajax/united_award_rank_list.htm?lotteryTypeId=1
 
  //--合买大奖排行
  //http://caipiao.taobao.com/lottery/order/united_rank_table.htm?type=DLT
 
  //--个人代购
  //http://caipiao.taobao.com/lottery/order/single_rank_table.htm?type=SSQ
 
  private int dataLinkPages = 0;
 
  private Set<String> unGetLinkUrls = new HashSet<String>();
 
  private String onsaleNo = "";
 
  private static Map<Integer,Integer> fullOrNotMap = new HashMap<Integer, Integer>();
  static{
    fullOrNotMap.put(1, 2);
    fullOrNotMap.put(2, 1);
  }

  public static void main(String[] args) throws IOException {
    CPHMTBService service = new CPHMTBService();
    CPHMWYService wyservice = new CPHMWYService();
//    service.writeTaobaoHMData("SSQ","2014028",false);
    /*for (int i = 2014031; i <= 2014031; i++) {
      Thread t = new Thread(new ServiceThread("SSQ",i+"",false));
      t.start();
    }*/
    Map<String,Object> last = null;
    Calendar c = Calendar.getInstance();
   
    popusers = null;
   
//    last = lastIssuoInfo("DLT");
//    int dltonsaleIssueNo = Integer.parseInt(last.get("onsaleIssueNo").toString());
//    service.onsaleNo = dltonsaleIssueNo+"";
   
//    try {
//      while(c.get(Calendar.HOUR_OF_DAY)<19||(c.get(Calendar.HOUR_OF_DAY)==18&&c.get(Calendar.MINUTE)<59)){
//        service.writeTaobaoHMData(dltonsaleIssueNo+"", "", true);
//        c = Calendar.getInstance();
//        service.tjHM4Taobao(dltonsaleIssueNo+"");
////       
//      }
//    } catch (InterruptedException e) {
//      e.printStackTrace();
//    } catch (ExecutionException e) {
//      e.printStackTrace();
//    }
//    service.tjHM4Taobao(dltonsaleIssueNo+"");
//    for (int i = 14056; i <= dltonsaleIssueNo-1; i++) {
//      try {
////        service.writeTaobaoHMData(i+"", "", dltonsaleIssueNo == i);
//        wyservice.setOnsale(dltonsaleIssueNo == i);
//        wyservice.writeWYHMData(""+i, dltonsaleIssueNo == i);
//      } catch (InterruptedException e) {
//        e.printStackTrace();
//      } catch (ExecutionException e) {
//        e.printStackTrace();
//      }
//    }
   
//    last = lastIssuoInfo("SSQ");
//    int ssqonsaleIssueNo = Integer.parseInt(last.get("onsaleIssueNo").toString());
//    service.onsaleNo = ssqonsaleIssueNo+"";
//    System.out.println(last.get("issueList"));
   
//    System.out.println(c.get(Calendar.HOUR_OF_DAY));
//    System.out.println(c.get(Calendar.MINUTE));
//    try {
////      while(c.get(Calendar.HOUR_OF_DAY)<19||(c.get(Calendar.HOUR_OF_DAY)==18&&c.get(Calendar.MINUTE)<59)){
//        service.writeTaobaoHMData(ssqonsaleIssueNo+"", "", true);
////        c = Calendar.getInstance();
////        service.tjHM4Taobao(ssqonsaleIssueNo+"");
//       
////        service.writeTaobaoHMData("2014060", "", false);
////        service.tjHM4Taobao("2014060");
////       
////      }
//    } catch (InterruptedException e) {
//      e.printStackTrace();
//    } catch (ExecutionException e) {
//      e.printStackTrace();
//    }
    service.tjHM4Taobao("14070");
    /*for (int i = 2013065; i <= 2013154; i++) {
      try {
        service.writeTaobaoHMData(i+"", "", ssqonsaleIssueNo == i);
        wyservice.setOnsale(ssqonsaleIssueNo == i);
//        wyservice.writeWYHMData(""+i, ssqonsaleIssueNo == i);
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }*/
   
   
    //service.readDetailLinkTaobao("SSQ", "2014031", 1, "", true);
   
//    service.tjHM4Taobao(2014058+"");
    /**/
  }
  public Map<String, Map<Integer, Integer>> tjHM4Taobao(String issueNo) throws IOException{
    Map<String, Map<Integer, Integer>> fullMap = tjHM4Taobao(issueNo,1);
    System.out.println(fullMap);
    if(issueNo.equals(onsaleNo)){
//      sendMail(issueNo,fullMap.toString());
    }
//    Map<String, Map<Integer, Integer>> notfullMap = tjHM4Taobao(issueNo,2);
//    System.out.println(notfullMap);
//   
//    Map<String, Map<Integer, Integer>> map = tjHM4Taobao(issueNo,100);
//    System.out.println(map);
    return null;
  }
  private Map<String, Map<Integer, Integer>> tjHM4Taobao(String issueNo,int is_not_full) throws IOException{
    Map<String, Map<Integer, Integer>> res = stat(issueNo, is_not_full, "tb");
    return res;
  }
 
  public void writeTaobaoHMData(String issueNo,String userName,boolean onsale) throws InterruptedException, ExecutionException{
    writeTaobaoHMData(issueNo, 1,userName,onsale);//full
//    writeTaobaoHMData(issueNo, 2,userName,onsale);
  }
 
  public void writeTaobaoHMData(String issueNo,int is_not_full,String userName,boolean onsale) throws InterruptedException, ExecutionException{
   
    String lotteryType = getLotteryType(issueNo, "tb");
    String fullOrNotStr = getFullOrNotName(is_not_full);
   
    Utils.print("****TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") dateLinks begin...");
    long b1 = System.currentTimeMillis();
    List<Map<String,String>> detailLinks = tbDataLinks(issueNo, is_not_full, userName, onsale);
    Utils.print("****TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") dateLinks ok..."+detailLinks.size()+"...time:"+(System.currentTimeMillis()-b1)+"ms");
//    int size = detailLinks.size();
    Utils.print("****TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") data begin...");
    long b2 = System.currentTimeMillis();
    List<String> dataList = tbData(detailLinks);
    Utils.print("****TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") data ok..."+dataList.size()+".time:"+(System.currentTimeMillis()-b2));
    Utils.print("****TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") data write begin...");
    long b3 = System.currentTimeMillis();
    StringBuilder cBuilder = new StringBuilder();
    for (String data : dataList) {
      cBuilder.append(data).append("\r\n");
    }
    Utils.print("****TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") data write ok..."+dataList.size()+".time:"+(System.currentTimeMillis()-b3));
    XmlUtils.writeConfigFile(cBuilder.toString(), "tb_"+issueNo+"_"+fullOrNotStr+".txt", false);
    Utils.print("TB issueNo: "+issueNo+"("+fullOrNotStr+") finished...links:"+detailLinks.size()+" records:"+dataList.size());
  }
 
  public static Map<String,Object> lastIssuoInfo(String lotteryType){
    String url = "http://caipiao.taobao.com//lottery/ajax/get_united_list.htm" +
        "?page=1&issue=&lotteryType="+lotteryType+"&playType=&commission_rate=-1"+
        "&confidential=1&united_fee=0-0&is_not_full=1&creator=&sort_obj=bonusRatio&sort=desc&change_sort=false"+
        "&lowAmount=0&highAmount=0&from=&onsale=&t="+System.currentTimeMillis();
    Document doc = null;
    try {
      doc = ConnectionManager.getInstance().getDocument(url);
    } catch (Exception e) {
     
    }
   
    String resultStr = doc.text();
    Map<String,Object> infoMap = new HashMap<String, Object>();
   
    JSONObject object = null;
    try {
      object = JSONObject.fromObject("{\""+resultStr.substring(resultStr.indexOf("totalItem")));
    } catch (Exception e) {
      System.out.println("get totalItem failed...");
    }
//    infoMap.put("totalItem", object.get("totalItem"));
//    infoMap.put("lotteryType", object.get("lotteryType"));
//    infoMap.put("totalPage", object.get("totalPage"));
    List<String> issueList = new ArrayList<String>();
    JSONArray arr = object.getJSONArray("issueList");
    for (Object object2 : arr) {
      JSONObject object3 = (JSONObject) object2;
      issueList.add(object3.getString("issue"));
      if (object3.getBoolean("onsale")) {
        infoMap.put("onsaleIssueNo", object3.get("issue"));
      }
    }
    infoMap.put("issueList", issueList);
    return infoMap;
  }
  // links 一级
  public List<Map<String,String>> dataLinks(String issueNo,int is_not_full,String userName,boolean onsale){
    List<Map<String,String>> linkList = new ArrayList<Map<String,String>>();
    int linkPages = dataLinkPages(issueNo, is_not_full, userName, onsale);
    for (int i = 0; i < linkPages; i++) {
      linkList.addAll(dataLinks(issueNo, is_not_full, userName, onsale, i));
    }
    return linkList;
  }
 
  public List<Map<String,String>> tbDataLinks(String issueNo,int is_not_full,String userName,boolean onsale) throws InterruptedException, ExecutionException{
    //ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    ForkJoinPool forkJoinPool = new ForkJoinPool(5);
    int detailLinkPages = dataLinkPages(issueNo, is_not_full, userName, onsale);
    TBDataLinkThread thread = new TBDataLinkThread(1,detailLinkPages,issueNo, is_not_full,userName,onsale,this);
        Future<List<Map<String,String>>> result = forkJoinPool.submit(thread);
        forkJoinPool.shutdown();
    return result.get();
  }
 
  public List<String> tbData(List<Map<String,String>> detailLinks) throws InterruptedException, ExecutionException{
    ForkJoinPool forkJoinPool = new ForkJoinPool(5);
    int size = detailLinks.size();
    TBDataThread thread = new TBDataThread(0,size-1,detailLinks,this);
        Future<List<String>> result = forkJoinPool.submit(thread);
        forkJoinPool.shutdown();
    return result.get();
  }
 
  private int dataLinkPages(String issueNo,int is_not_full,String userName,boolean onsale){
    int linkPages = 0;
    String lotteryType = getLotteryType(issueNo, "tb");
    is_not_full = fullOrNotMap.get(is_not_full);
    if(is_not_full == 1&&!onsale){
      is_not_full = 4;
    }
    String url = "http://caipiao.taobao.com//lottery/ajax/get_united_list.htm" +
        "?page=1&issue="+issueNo+"&lotteryType="+lotteryType+"&playType=&commission_rate=-1"+
        "&confidential=1&united_fee=0-0&is_not_full="+is_not_full+"&creator="+userName+"&sort_obj=bonusRatio&sort=desc&change_sort=false"+
        "&lowAmount=0&highAmount=0&from=&onsale="+onsale+"&t="+System.currentTimeMillis();
    Document doc = null;
    try {
      doc = ConnectionManager.getInstance().getDocument(url);
    } catch (Exception e) {
      logger.error("connect fail..", e);
      return 0;
    }
    String resultStr = doc.text();
    if (resultStr.indexOf("totalPage") == -1) {
      logger.error("content get failed(获取元素失败).."+url);
      return 0;
    }
    linkPages = Integer.parseInt(resultStr.substring(resultStr.indexOf("totalPage")+11, resultStr.lastIndexOf("sale")-2));
    dataLinkPages = linkPages;
    return linkPages;
  }
  /**
   * @param game
   * @param issueNo
   * @param is_not_full
   * @return detailLink 详情界面链接
   */
  public  List<Map<String,String>> dataLinks(String issueNo,int is_not_full,String userName,boolean onsale,int pageNo){
    String fullOrNotStr = getFullOrNotName(is_not_full);
    String lotteryType = getLotteryType(issueNo, "tb");
    is_not_full = fullOrNotMap.get(is_not_full);
    if(is_not_full == 1&&!onsale){
      is_not_full = 4;
    }
    /*--ssq 未满员 不限佣金 内容公开(返奖率 排序 sort_obj=unitedRoi)
    http://caipiao.taobao.com/lottery/ajax/get_united_list.htm?page=1&issue=&lotteryType=SSQ&playType=&commission_rate=-1
    &confidential=1&united_fee=0-0&is_not_full=1&creator=&sort_obj=process&sort=desc&change_sort=false
    &lowAmount=0&highAmount=0&from=&onsale=&t=1394339345261*/
    //is_not_full 2:已满员   1:未满员 //bonusRatio  返奖率
    String url = "http://caipiao.taobao.com//lottery/ajax/get_united_list.htm" +
        "?page="+pageNo+"&issue="+issueNo+"&lotteryType="+lotteryType+"&playType=&commission_rate=-1"+
        "&confidential=1&united_fee=0-0&is_not_full="+is_not_full+"&creator="+userName+"&sort_obj=bonusRatio&sort=desc&change_sort=false"+
        "&lowAmount=0&highAmount=0&from=&onsale="+onsale+"&t="+System.currentTimeMillis();
    List<Map<String,String>> resList = detailLink(url);
    Utils.print("********TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") dataLinks pageNo:"+pageNo+" totalPage:"+dataLinkPages);
    if (pageNo%30==0) {
      Utils.print("********TB "+lotteryType+" "+issueNo+"("+fullOrNotStr+") dataLinks pageNo:"+pageNo+" totalPage:"+dataLinkPages);
    }
    return resList;
  }
 
  private  List<Map<String,String>> detailLink(String url){
    Document doc = null;
    try {
      doc = ConnectionManager.getInstance().getDocument(url);
    } catch (Exception e) {
      logger.error("connect fail..", e);
      return null;
    }
    List<Map<String,String>> resList = new ArrayList<Map<String,String>>();
    if (doc == null) {
      unGetLinkUrls.add(url);
      logger.error("content get failed(doc == null).."+url);
      return resList;
    }
    String resultStr = doc.text();
    resList.addAll(getDetailLinkes(resultStr));
    return resList;
  }
 
  //{"progress":100,"amountMoney":"25678.00","status":2,"minBuy":"0.00","issueId":11055568,
//  "bonusRatio":29.96,"playType":0,"lotteryTypeCode":"SSQ","remainCount":0,"commision":10,
//  "id":"SAIS4GJCL7MPIKWCF6QECQY2JM",
//  "detailLink":"http://caipiao.taobao.com/lottery/order/united_detail.htm?united_id=SAIS4GJCL7MPIKWCF6QECQY2JM",
//  "title":"继续守号!加群32936760/85596495",
//  "userLink":"http://caipiao.taobao.com/lottery/experience/united.htm?user_id=CS6J2Y247TPEM&lottery_type=1",
//  "userId":"CS6J2Y247TPEM","seq":1,"userScore":1659612220,"userName":"四海数码2003","bonus":"2575.00",
//  "unitMoney":"1.00","confidential":1}
 
  public List<Map<String,String>> getDetailLinkes(String res){
    List<Map<String,String>> list = new ArrayList<Map<String,String>>();
    Map<String,String> map = null;
    try {
      if (res.indexOf("detailLink")>-1) {
        map = new HashMap<String,String>();
        String detailLink = res.substring(res.indexOf("detailLink")+13, res.indexOf("title")-3);
        String amountMoney = res.substring(res.indexOf("amountMoney")+14, res.indexOf("status")-3);
        String bonusRatio = res.substring(res.indexOf("bonusRatio")+12, res.indexOf("playType")-3);
        String userScore = res.substring(res.indexOf("userScore")+11, res.indexOf("userName")-3);
        res = res.substring(res.indexOf("userName"));
        String userName = res.substring(res.indexOf("userName")+11, res.indexOf("bonus")-3);
       
        map.put("amountMoney", amountMoney);
        map.put("detailLink", detailLink);
        map.put("dataLink", dataLink(detailLink));
        map.put("bonusRatio", bonusRatio);
        map.put("userScore", userScore);
        map.put("userName", userName);
        list.add(map);
        if(res.indexOf("progress")>-1){
          res = res.substring(res.indexOf("progress"));
        }
       
      }
      if (res.length()>0) {
        if (res.indexOf("detailLink")>-1) {
          list.addAll(getDetailLinkes(res));
        }
      }
    } catch (Exception e) {
      System.err.println("links get error..");
      e.printStackTrace();
    }
   
    return list;
  }
 
  /*public List<String> getDetailLinkes(String res){
    List<String> list = new ArrayList<String>();
    try {
      if (res.indexOf("detailLink")>-1) {
        String detailLink = res.substring(res.indexOf("detailLink")+13, res.indexOf("title")-3);
       
        res = res.substring(res.indexOf("userName"));
       
        String userName = res.substring(res.indexOf("userName")+11, res.indexOf("bonus")-3);
//        System.out.println(userName);
        if(res.indexOf("progress")>-1){
          res = res.substring(res.indexOf("progress"));
        }
       
        if(popusers == null || (popusers.length>0&&Arrays.asList(popusers).contains(userName))){
          list.add(detailLink);
        }
      }
      if (res.length()>0) {
        if (res.indexOf("detailLink")>-1) {
          list.addAll(getDetailLinkes(res));
        }
      }
    } catch (Exception e) {
      System.err.println("links get error..");
      e.printStackTrace();
    }
   
    return list;
  }*/
  // data link
  private String dataLink(String detailLink){
    Document doc = null;
    try {
      doc = ConnectionManager.getInstance().getDocument(detailLink);
    } catch (Exception e) {
      logger.error("connect fail..", e);
      return null;
    }
    Elements tds = doc.getElementsByAttributeValue("class", "scheme-content");
    if (tds == null) {
      logger.error("content get failed(获取元素失败).."+detailLink);
      return null;
    }
    //<a id="J_CreatorNameLink" href="http://caipiao.taobao.com/lottery/experience/united.htm?user_id=SIDXSXWYYSJXC&amp;lottery_type=1" target="_blank" hidefocus="true" title="他郎酒馆">他郎酒馆</a>
//    Element userLink = doc.getElementById("J_CreatorNameLink");
//    String userName = "";
//    if(userLink!=null){
//      userName = userLink.attr("title");
//    }
    String dataLink = "";
    if (tds.size()>0) {
//      if(StringUtils.isNotBlank(userName)&&Arrays.asList(popusers).contains(userName)){
        dataLink = tds.get(0).getElementsByTag("iframe").get(0).attr("src");
//      }
    }
    return dataLink;
  }
 
 
  private int dataPages(String dataLink){
    String url = getDataLink(dataLink,1);
    Document doc = null;
    try {
      doc = ConnectionManager.getInstance().getDocument(url);
    } catch (Exception e1) {
      logger.error("connect fail..", e1);
      return 0;
    }
    Elements pageEs = doc.getElementsByAttributeValue("class", "page-info");
    if (pageEs.size()>0) {
      String pageInfo = pageEs.get(0).ownText();
      int pages = Integer.parseInt(pageInfo.substring(pageInfo.indexOf("/")+1));
      return pages;
    }
    return 0;
  }
 
  public List<String> tbData(Map<String,String> dataLink){
    List<String> resList = new ArrayList<String>();
    int dataPages = dataPages(dataLink.get("dataLink"));
    for (int i = 1; i <= dataPages; i++) {
      resList.addAll(tbData(dataLink, i));
    }
    return resList;
  }
 
  private List<String> tbData(Map<String,String> dataLink,int dataPageNo){
    String url = getDataLink(dataLink.get("dataLink"),dataPageNo);
    Document doc = null;
    try {
      doc = ConnectionManager.getInstance().getDocument(url);
    } catch (Exception e1) {
      logger.error("connect fail..", e1);
      return null;
    }
    List<String> resList = new ArrayList<String>();
    Element div = doc.getElementById("select-detail");
    Elements tds = div.getElementsByAttributeValue("class", "td2");
    if (tds!=null&&tds.size()>0) {
      for (Element e : tds) {
        String data = e.ownText();
        if (data.lastIndexOf("单式")>-1) {
          data = data.substring(0, data.lastIndexOf("单式"));
        }
        if (data.lastIndexOf("胆拖")>-1) {
          data = data.substring(0, data.lastIndexOf("胆拖"));
        }
        if (data.lastIndexOf("复式")>-1) {
          data = data.substring(0, data.lastIndexOf("复式"));
        }
        if (data.lastIndexOf("普通")>-1) {
          data = data.substring(0, data.lastIndexOf("普通"));
        }
        data = dataLink.get("userName")+"||"
            +dataLink.get("userScore")+"||"
            +dataLink.get("bonusRatio")+"||"
            +dataLink.get("amountMoney")+"||"
            //+dataLink.get("dataLink")+"||"
            //+dataLink.get("detailLink")+"||"
            +data.trim();
//        System.out.println(data);
        resList.add(data);
      }
    }
    return resList;
  }
 
  private String getDataLink(String dataLink,int dataPageNo){
    /*String url = "http://caipiao.taobao.com/lottery/order/united_order_detail_szc.htm?" +
    "_tb_token_=e1bd7335514eb&page="+pageNo +
    "&tb_united_id=" + tb_united_id +
    "&lottery_type_id=1" +
    "&issue_id="+issue_id+"&db_type=0&is_history=false";*/
    return "http://caipiao.taobao.com/lottery/order/"+dataLink+"&page="+dataPageNo;
  }
  @Override
  public List<String> dataLinks(int pageNo) {
    // TODO Auto-generated method stub
    return null;
  }
  @Override
  public List<String> data(String dataLink) {
    // TODO Auto-generated method stub
    return null;
  }
}
class TBDataLinkThread extends RecursiveTask<List<Map<String,String>>>{
  private static final long serialVersionUID = 1L;
  private static final int THRESHOLD = 30;
    private int start;
    private int end;
    private String issueNo;
    private int full_flag;
    private String userName;
    private boolean onsale;
   
    private CPHMTBService tbService = null;
   
  public TBDataLinkThread(int start, int end,String issueNo,int full_flag,String userName,boolean onsale,CPHMTBService tbService) {
        this.start = start;
        this.end = end;
        this.issueNo = issueNo;
        this.full_flag = full_flag;
        this.tbService = tbService;
        this.userName = userName;
        this.onsale = onsale;
    }

  @Override
  protected List<Map<String,String>> compute() {
    List<Map<String,String>> alllist = new ArrayList<Map<String,String>>();
    List<Map<String,String>> list = null;;
        if((end - start) < THRESHOLD){
            for(int i = start; i<= end;i++){
              list = tbService.dataLinks(issueNo, full_flag, userName, onsale,i);
                alllist.addAll(list);
            }
            //Utils.print("********TB HMLinks...("+start+"--"+end+")->"+alllist.size());
            return alllist;
        }
       
        int middle = (start + end) /2;
        TBDataLinkThread left = new TBDataLinkThread(start, middle,issueNo, full_flag,userName,onsale,tbService);
        TBDataLinkThread right = new TBDataLinkThread(middle + 1, end,issueNo, full_flag,userName,onsale,tbService);
        left.fork();
        right.fork();
        alllist.addAll(left.join());
        alllist.addAll(right.join());
        return alllist;
  }
 
}
class TBDataThread extends RecursiveTask<List<String>>{
  private static final long serialVersionUID = 1L;
  private static final int THRESHOLD = 500;
    private int start;
    private int end;
    private List<Map<String,String>> detailLinks;
    private CPHMTBService tbService = null;
   
  public TBDataThread(int start, int end,List<Map<String,String>> detailLinks,CPHMTBService tbService) {
        this.start = start;
        this.end = end;
        this.detailLinks = detailLinks;
        this.tbService = tbService;
    }

  @Override
  protected List<String> compute() {
    List<String> alllist = new ArrayList<String>();
    List<String> list = null;;
        if((end - start) < THRESHOLD){
            for(int i = start; i<= end;i++){
              list = getData(detailLinks.get(i));
                alllist.addAll(list);
            }
            //if (alllist.size()%200==0) {
              Utils.print("********TB HMData...("+start+"--"+end+")->"+alllist.size());
      //}
            return alllist;
        }
        int middle = (start + end) /2;
        TBDataThread left = new TBDataThread(start, middle,detailLinks,tbService);
        TBDataThread right = new TBDataThread(middle + 1, end,detailLinks,tbService);
        left.fork();
        right.fork();
        alllist.addAll(left.join());
        alllist.addAll(right.join());
        return alllist;
  }
 
  private List<String> getData(Map<String,String> detailLink){
    return tbService.tbData(detailLink);
  }
 
}
TOP

Related Classes of cputils.TBDataThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.