package com.qa.framework.library.multithread;

import com.qa.framework.bean.DataConfig;
import com.qa.framework.bean.TestData;
import com.qa.framework.core.DataConvertor;
import com.qa.framework.core.ParamValueProcessor;
import com.qa.framework.core.TestXmlData;
import com.qa.framework.library.base.IOHelper;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by Administrator on 2016/9/13.
 */
public class WorkerThread implements Runnable {
    protected static final Logger logger = Logger.getLogger(WorkerThread.class);
    private List<String> files = TestXmlData.getTestCaseFiles();
    private  List<Object[]> xmldata = new ArrayList<Object[]>();

    @Override
    public void run() {
        String filePath;
        String fileName;
        synchronized (files) {
               filePath=this.files.get(0);
               fileName=IOHelper.getName(filePath);
            this.files.remove(0);
        }
        logger.info("---->" + Thread.currentThread().getName() + " Start. Command = " + fileName);
        DataConvertor dataConvertor = new DataConvertor(filePath);
        DataConfig dataConfig = dataConvertor.getDataConfig();
        ParamValueProcessor paramValueProcessor = new ParamValueProcessor(dataConfig);
        paramValueProcessor.process();
        for (TestData data : dataConfig.getTestDataList()) {
            Object[] d = {data, dataConfig.getUrl(), dataConfig.getHttpMethod()};
            this.xmldata.add(d);
        }
        logger.info("---->"+Thread.currentThread().getName()+fileName+" End.");
    }
    public static List<Object[]> handleXml() throws InterruptedException{
        //Get the ThreadFactory implementation to use
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //creating the ThreadPoolExecutor
        List<String> files = TestXmlData.getTestCaseFiles();
        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(10, files.size()-20, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(20),
                threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        //start the monitoring thread
        MonitorThread monitor = new MonitorThread(executorPool, 1);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        //submit work to the thread pool
        WorkerThread workerThread=new WorkerThread();
        for(int i=0;i<files.size();i++){
            Thread t=new Thread(workerThread);
            executorPool.execute(t);
        }
        try{
            monitorThread.join();
        }catch (Exception e){
            e.printStackTrace();
        }
        return workerThread.getXmlDate();
    }
    public  List<Object[]> getXmlDate(){
        return xmldata;
    }

}

@@ -1688,16 +1688,41 @@
 eption%7B%0A
+        int maxPoolSize;%0A
         
@@ -1938,24 +1938,147 @@
 aseFiles();%0A
+        if (files.size()%3E20)%7B%0A            maxPoolSize=files.size()-20;%0A        %7Delse%7B%0A            maxPoolSize=5;%0A        %7D%0A
         Thre
@@ -2134,31 +2134,27 @@
 tor(10, 
-files.size()-20
+maxPoolSize
 , 10, Ti

update maxPoolSize