java異步線程池同時請求多個接口數據


 

本人開發的開發者技術變現資源聚集地,大家支持下,下面是網址

 

https://www.baiydu.com

 

 

 

 

 一、主要使用類

     1. ExecutorService

      java線程池類

     申明方式:ExecutorService exc = Executors.newFixedThreadPool(requestParameterArray.length()); 

     參數:requestParameterArray.length()是請求線程的總數量,其中每一個成員存放單個線程所需參數。

    代碼:

    2.Future

      Future是一個接口,他提供給了我們方法來檢測當前的任務是否已經結束,還可以等待任務結束並且拿到一個結果,通過調用Future的get()方法可以當任務結束后返回一個結果值,如果線程里的任何一個線程工作沒有結束,則線程會自動阻塞,直到任務執行完畢,我們可以通過調用cancel()方法來停止一個任務,如果任務已經停止,則cancel()方法會返回true;如果任務已經完成或者已經停止了或者這個任務無法停止,則cancel()會返回一個false。當一個任務被成功停止后,他無法再次執行。isDone()和isCancel()方法可以判斷當前工作是否完成和是否取消,他的作用通過callable的回調獲得我們請求的結果。

    ExecutorService/Future的執行代碼類

   

public class AAAThreadHttpRequest
{ 
    
      //首頁返回所需的參數和接口,電影接口
    //熱門電影
    private String hotfilmUrl = "http://expand.video.iqiyi.com/api/top/list.json?apiKey=?&topType=1&categoryId=1&limit=30&sr=1";
 //熱門電視劇
    private String hotdianshijuUrl = "http://expand.video.iqiyi.com/api/top/list.json?apiKey=?&topType=1&categoryId=2&limit=5&sr=2";
 //熱門動漫
    private String hotanimationUrl = "http://expand.video.iqiyi.com/api/top/list.json?apiKey=?&topType=1&categoryId=4&limit=5&sr=3";
 //第一個分段數據
     private String segmentOneUrl ="http://expand.video.iqiyi.com/api/top/list.json?apiKey=?&topType=1&categoryId=7&limit=6&sr=5";
     
     //淘寶客 
     
 
public      LinkedList<JSONObject>  ShiPinThreadHandle() throws JSONException, IOException, InterruptedException, ExecutionException
{
     //組合線程請求參數
    JSONArray requestParameterArray=new JSONArray();
     JSONObject  a=new JSONObject();
    a.put("requestUrl", hotfilmUrl);
    a.put("dataType", "hotFilm");
    a.put("type", "shipin");
    
    JSONObject  a1=new JSONObject();
    a1.put("requestUrl", hotdianshijuUrl);
    a1.put("dataType", "hotDianshiju");
    a1.put("type", "shipin");
    
    JSONObject  a2=new JSONObject();
    a2.put("requestUrl", hotanimationUrl);
    a2.put("dataType", "hotDongman");
    a2.put("type", "shipin");
    
 
    JSONObject  a3=new JSONObject();
    a3.put("requestUrl", segmentOneUrl);
    a3.put("dataType", "firstSegmentData");
    a3.put("type", "shipin");
    
    
    
   
    requestParameterArray.put(a);
    requestParameterArray.put(a1);
    requestParameterArray.put(a2);
    requestParameterArray.put(a3);
    
      //申明線程池
   ExecutorService exc = Executors.newFixedThreadPool(requestParameterArray.length());  
   //申明數據回調處理類List<Future<JSONObject>>
   List<Future<JSONObject>> futures = new ArrayList<Future< JSONObject>>();  
    for (int i =0; i < requestParameterArray.length(); i++) {  
     
       JSONObject singleobje=requestParameterArray.getJSONObject(i);
        //申請單個線程執行類
       ShiPinThreadHandleRequest call =new ShiPinThreadHandleRequest(singleobje);  
       //提交單個線程
      Future< JSONObject> future = exc.submit(call);  
      //將每個線程放入線程集合, 這里如果任何一個線程的執行結果沒有回調,線程都會自動堵塞
      futures.add(future);  

   }  
   //所有線程執行完畢之后會執行下面的循環,然后通過循環每個個線程后執行線程的get()方法每個線程執行的結果
   for (Future< JSONObject> future : futures) {     
  
    JSONObject json= future.get();
    
     AAAANewAppShareSingleton.getInstance().homePageSessionDictionary.put(json.getString("dataType"), json.getJSONArray("returnData"));
 
   }  
   AAAANewAppShareSingleton.getInstance().homeIsOrNoReturn=1;
   
   //關閉線程池
   exc.shutdown();  
  
  
   
   //這里由於我直接將返回結果放入到單利中緩存了,所有返回null
    return null;
     
}

 

   3.Callable

    線程執行者,我們的數據將在這個類的構造函數里面執行,這個類自帶了回調函數。當執行結果返回時會通過它自帶的回調將請求結果反饋給Future。

   Callable執行代碼類

   

public   class ShiPinThreadHandleRequest implements Callable<JSONObject> {  

      private  JSONObject parameter;
        
        public ShiPinThreadHandleRequest(JSONObject parameter) throws JSONException, IOException {  
             this.parameter=parameter;
        
              try
             {
       
             String HtmlJson=httpGetRequest(this.parameter.getString("requestUrl"));
             JSONObject object=new JSONObject(HtmlJson);
             //請求的愛奇藝接口
            if(this.parameter.get("type").equals("shipin"))
            {
                JSONArray returnArray=object.getJSONArray("data");
            
                 
                
                
                  
                if(this.parameter.getString("dataType").equals("firstSegmentData"))
                {
                    
                     JSONArray againArray=new JSONArray();
                for (int j=0;j<returnArray.length();j++)
                {
                     JSONArray tempArrray=new JSONArray();
                    tempArrray.put(returnArray.getJSONObject(j));
                    tempArrray.put(returnArray.getJSONObject(j+1));

                    againArray.put(tempArrray);
                    j=j+1;

                }
                this.parameter.put("returnData",againArray);
                }
                else
                {
                      this.parameter.put("returnData",returnArray);
                }
                 
               
            }
          //請求的淘寶客接口
            else
            {
                
            }
            
              }
            catch(Exception e)
             {
                 
             }
             
          
      
    }  
  
    //數據回調
    public JSONObject call() throws Exception {  
       
       
        return this.parameter;  
    }
}

  4.http請求方法

    

public    String httpGetRequest(String urlString1) {
     
    String result = "";
    BufferedReader in = null;
    
    try {
      
        URL realUrl = new URL(urlString1);
        // 打開和URL之間的連接
        URLConnection connection = realUrl.openConnection();
        // 設置通用的請求屬性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 建立實際的連接
        connection.connect();
        // 獲取所有響應頭字段
        Map<String, List<String>> map = connection.getHeaderFields();
        // 遍歷所有的響應頭字段
        for (String key : map.keySet()) {
           
        }
        // 定義 BufferedReader輸入流來讀取URL的響應
        in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        System.out.println("發送GET請求出現異常!" + e);
        e.printStackTrace();
    }
    // 使用finally塊來關閉輸入流
    finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
    return result;

}

  二、適合的使用場景

      復雜的網頁爬蟲,如要同時請求多個不同網頁的數據,並且需要執行不同的數據處理,這個是非常合適的,執行線程傳遞的參數到最后callback是會附帶一起反饋,你可以根據請求時的附帶的類型參數進行判斷。 復雜的首頁數據,同時需要請求不同數據庫的不同接口。

三、優勢

      解決了多線程中復雜的線程堵塞問題,因為有future,它已經給你做了所有的事。

 

 

 

 

 

本人創業做的一款androidApp, 下載量已經有2000多萬,各種當前熱門的網絡手機獎勵紅包全部集成,另外還有熱門電影和淘寶高額優惠券!很適合各類型的用戶。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM