package com.meread;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.fluent.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by yangxg on 16/2/18.
 */
public class DomainTest {
    static int processed = 0;

    private static final Logger logger = LoggerFactory.getLogger(DomainTest.class);

    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("start..");
        File domain_result = new File(DomainTest.class.getClassLoader().getResource("tmp/domain_result.txt").getFile());
        final File result = new File(DomainTest.class.getClassLoader().getResource("tmp/result.txt").getFile());
        FileUtils.write(result, "", false);
//        String s = FileUtils.readFileToString(cn);
//
//        for (char c : s.toCharArray()) {
//            if (!Character.valueOf(c).toString().trim().equals("")) {
//                FileUtils.write(cn_result, c + System.getProperty("line.separator"), true);
//            }
//        }
//        List<String> lines = FileUtils.readLines(cn_result);
//        for (String line : lines) {
//            String pinyin = PinyinHelper.convertToPinyinString(line.trim(), ",", PinyinFormat.WITHOUT_TONE);
//            FileUtils.write(pinyin_result, pinyin + System.getProperty("line.separator"), true);
//        }

//        List<String> lines = FileUtils.readLines(pinyin_result);
//        CombinationIterator<String> ci = new CombinationIterator<String>(lines, 2);
//
//        while (ci.hasNext()) {
//            List<String> next = ci.next();
//            FileUtils.write(domain_result, next.get(0) + next.get(1) + ".com" + System.getProperty("line.separator"), true);
//        }
        int threadSize = 2;

        final List<String> lines = FileUtils.readLines(domain_result);
        final int total = lines.size();
        List<List<String>> partition = Lists.partition(lines, threadSize);
        ExecutorService es = Executors.newFixedThreadPool(threadSize);
        for (final List<String> process : partition) {
            es.execute(new Runnable() {
                @Override
                public void run() {
                    List<List<String>> subProcesses = Lists.partition(process, 10);
                    for (List<String> subProcess : subProcesses) {
                        StringBuilder sb = new StringBuilder(50);
                        for (String s : subProcess) {
                            sb.append(s).append(",");
                        }
                        String url = "http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + sb;
                        try {
                            String response = Request.Get(url).execute().returnContent().asString();
                            FileUtils.write(result, response + System.getProperty("line.separator"), true);
                            processed += 10;
                            logger.info(total + ":" + processed + " : " + response);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        es.awaitTermination(10, TimeUnit.DAYS);
    }
}

@@ -273,130 +273,8 @@
 ist;
-%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A/*
@@ -817,52 +817,8 @@
 ));%0A
-        FileUtils.write(result, %22%22, false);%0A
 //  
@@ -1807,37 +1807,8 @@
   %7D%0A
-        int threadSize = 2;%0A%0A
     
@@ -1944,323 +1944,8 @@
 ng%3E%3E
- partition = Lists.partition(lines, threadSize);%0A        ExecutorService es = Executors.newFixedThreadPool(threadSize);%0A        for (final List%3CString%3E process : partition) %7B%0A            es.execute(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    List%3CList%3CString%3E%3E
  sub
@@ -1976,22 +1976,20 @@
 ion(
-proces
+line
 s, 10);%0A
     
@@ -1980,36 +1980,25 @@
 lines, 10);%0A
-            
+%0A
         for 
@@ -2048,36 +2048,24 @@
             
- 
-           
 StringBuilde
@@ -2106,28 +2106,16 @@
         
-           
- 
 for (Str
@@ -2152,28 +2152,16 @@
         
-          
-  
 sb.appen
@@ -2194,34 +2194,10 @@
     
-        
-    %7D%0A            
+%7D%0A
     
@@ -2296,38 +2296,14 @@
     
-     
-       try %7B%0A            
+try %7B%0A
     
@@ -2399,36 +2399,24 @@
             
-  
-          
 FileUtils.wr
@@ -2499,28 +2499,16 @@
         
-           
- 
 processe
@@ -2516,28 +2516,16 @@
  += 10;%0A
-            
         
@@ -2593,32 +2593,132 @@
 e);%0A            
+    lines.removeAll(subProcess);%0A                FileUtils.writeLines(domain_result, lines, false);%0A
             %7D ca
@@ -2755,28 +2755,16 @@
         
-            
 e.printS
@@ -2784,36 +2784,24 @@
             
- 
-           
 %7D%0A          
@@ -2806,35 +2806,71 @@
     
-        %7D%0A                %7D
+if (processed %25 200 == 0) %7B%0A                Thread.sleep(3000);
 %0A   
@@ -2879,18 +2879,16 @@
        %7D
-);
 %0A       
@@ -2894,56 +2894,8 @@
   %7D%0A
-        es.awaitTermination(10, TimeUnit.DAYS);%0A
     

add word count example