스레드 스레드로 일부 메소드를 실행 한 후에는 실행이 성공했는지 여부를 결정해야합니다.
public void run () {}
run () 메소드의 반환 값은 비어 있습니다. 어떻게해야합니까?
해결책:
암호:
package com.example.thread; import java.io.ioexception; import java.io.inputStream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.concurrent.callable; import com.Stool.log; example.Stream. 클래스 Mythread1은 Callable <string> {private static final String tag = "xxxyyy"를 구현합니다. 개인 문자열 전화; 개인 문자열 이름; public mythread1 (문자열 이름, 문자열 전화) {this.name = 이름; this.phone = 전화; } // public mythread1 (문자열 이름, 문자열 전화) {// super (name); // this.phone = phone; //} // public void run () {// log.i (tag, thread.currentthread (). getName () + "); // string newPath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + phone + "& flow = t & callback = dp._jsonprequest._5"; // stringBuilder strbui = new StringBuilder (newPath); // try {// url url = url (strbui.tostring ()); // httpurlConnection con = (httpurlConnection) url.openConnection (); // con.addrequestProperty ( "content-type", "text/html; charset = utf-8"); // con.addrequestpy (// con.addrequestpepe. "Mozilla/5.0 (Windows NT 6.1) Applewebkit/537.36 (Gecko Like Gecko) Chrome/38.0.2125.104 Safari/537.36"); // addrequestproperty ( "Cookie", "_hc.v =/"// "0ffCDF91-A4D6-4B08-8800-3D59D2FFE45.1413899372 ///"/"; abtest =/"48,124 // | 52,133 // | 47,122 // | 44,116 // "45,115/"; __utma = 1.29319792.1412090870.1413609669.1413645970.5; _tr.u = 7dhy0yfhd9ry065f = 17; 13.t = 17dea7d2a7d8e46d84bf12a74627b72; _HC.V =/"///"1BDAD871-405E-46E3-97D6-46571AD48E14.1413897928 // "/"; jsessionID = 891671FC6B450418DD6E0611A3E4CF48 "); // con.addRequestProperty ("referenter ","http://t.dianping.com/register") ;// Con.setConnectTimeout (5000); // // ////////////////////////////////// (con.getResponseCode () == 200) {// inputStream inputstr = con.getInputStream (); // string info = new String (streamTool.read (inputstr), "utf-8"); // log.i (tag, thread.currentthread (). 자동 유래 캐치 블록 // e.printstacktrace (); //} catch (ioException e) {// // todo 자동 유래 된 캐치 블록 // e.printstacktrace (); //} catch (ioexception e) {// // to auto-generated catch // e.printstacktrace (//); //}; 자동 생성 캐치 블록 // e.printstacktrace (); //} // //} @override public String () rows exception {log.i (tag, thread.currentthread (). getName () + "...... start"); 문자열 newPath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + 폰 + "& flow = t & callback = dp._jsonprequest._5"; StringBuilder strbui = New StringBuilder (NewPath); try {url url = new url (strbui.tostring ()); httpurlconnection con = (httpurlConnection) url.openConnection (); con.addrequestProperty ( "content-type", "text/html; charset = utf-8"); Con.addrequestProperty ( "사용자 에이전트", "Mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (Gecko Like Gecko) Chrome/38.0.2125.104 Safari/537.36"); con.addrequestProperty ( "쿠키", "_hc.v =/"/// "0FFCDF91-A4D6-4B08-8800-3D59D2E2FE45.141389372 //"/"; abtest =/"48,124 // | 52,133 // | 47,122 // 44,106 // 45,115/"; __utmz = 1.1413645970.5.2. utmcsr = t.dianping.com | utmcmd = utmcct =/xian; t_rct = 2172824 | 2171729 | 6457678; 13.ts = CD3BD6495620E2A204912C4BDFD43220; _hc.v =/"//"1BDAD871-405E-46E3-97D6-46571AD48E14.1413897928 // "/"; jsessionID = 891671FC6B450418DD6E0611A3E4CF48 "); con.addrequestProperty ( "참조", "http://t.dianping.com/register"); Con.setConnectTimeout (5000); con.setRequestMethod ( "get"); float x = 3.4f; if (con.getResponseCode () == 200) {inputStream inputstr = con.getInputStream (); 문자열 정보 = 새 문자열 (streamTool.Read (inputStr), "UTF-8"); log.i (tag, thread.currentthread (). getName () + info); if (info.contains ( "200")) "200"을 반환합니다. }} catch (marformedurlexception e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } catch (ioexception e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } catch (예외 e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } // todo 자동 생성 메소드 스터브 리턴 "404"; }} 반환 값을 처리하십시오
executorService exec = executors.newCachedThreadPool (); ArrayList <future <문자열 >> results = new arrayList <future <string>> (); for (int i = 0; i <num; i ++) results.add (exec.submit (selectthread (telnumber))); for (future <string> fs : results) try {if (fs.get (). equals ( "200")) success ++; } catch (InterruptedException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } catch (executionException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } 반환 성공;위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.