本人开发的开发者技术变现资源汇集地,你们支持下,下面是网址java
https://www.baiydu.comandroid
1、主要使用类数据库
1. ExecutorServicejson
java线程池类网页爬虫
申明方式:ExecutorService exc = Executors.newFixedThreadPool(requestParameterArray.length()); api
参数:requestParameterArray.length()是请求线程的总数量,其中每个成员存放单个线程所需参数。缓存
代码:网络
2.Future多线程
Future是一个接口,他提供给了咱们方法来检测当前的任务是否已经结束,还能够等待任务结束而且拿到一个结果,经过调用Future的get()方法能够当任务结束后返回一个结果值,若是线程里的任何一个线程工做没有结束,则线程会自动阻塞,直到任务执行完毕,咱们能够经过调用cancel()方法来中止一个任务,若是任务已经中止,则cancel()方法会返回true;若是任务已经完成或者已经中止了或者这个任务没法中止,则cancel()会返回一个false。当一个任务被成功中止后,他没法再次执行。isDone()和isCancel()方法能够判断当前工做是否完成和是否取消,他的做用经过callable的回调得到咱们请求的结果。ide
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; }
2、适合的使用场景
复杂的网页爬虫,如要同时请求多个不一样网页的数据,而且须要执行不一样的数据处理,这个是很是合适的,执行线程传递的参数到最后callback是会附带一块儿反馈,你能够根据请求时的附带的类型参数进行判断。 复杂的首页数据,同时须要请求不一样数据库的不一样接口。
3、优点
解决了多线程中复杂的线程堵塞问题,由于有future,它已经给你作了全部的事。
本人创业作的一款androidApp, 下载量已经有2000多万,各类当前热门的网络手机奖励红包所有集成,另外还有热门电影和淘宝高额优惠券!很适合各种型的用户。