Nachdem Sie einige Methoden mit Thread -Thread ausgeführt haben, müssen Sie feststellen, ob die Ausführung erfolgreich ist.
public void run () {}
Der Rückgabewert der Run () -Methode ist leer. Was soll ich tun?
Lösung:
Code:
paket com.example.thread; import java.io.ioxception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformaledurlexception; import Java.net.url; importieren java.util.concurrent importierbar und roid.util und Roroid.util; com.example.streamtool.streamtool; öffentliche Klasse mythead1 implementiert Callable <string> {private statische final String tag = "xxxyyy"; privates String -Telefon; privater Zeichenfolge Name; public myThread1 (String -Name, String -Telefon) {this.name = name; this.phone = Telefon; } // public mythread1 (String -Name, String -Telefon) {// Super (Name); // this.phone = Telefon; //} // public void run () {// log.i (Tag, Thread.currentThread (). "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + Telefon + "& Flow = T & Callback = dp._jsonprequest._5"; // Stringbuilder strbui = new StringBuilder (NewPath); ///// // try {/ // // // // // // // // // // // // // // // // url = neue neue Newpon (New Path); Url (strbui.toString ()); // httpurlConnection con = (httpurlConnection) url.openconnection (); // con.addrequestProperty ("Content-Type", "text/html; charset = utf-8"); "Mozilla/5.0 (Windows NT 6.1) Applewebkit/537.36 (khtml, wie Gecko) Chrome/38.0.2125.104 Safari/537.36"); // Con.AddrequestProperty ("Cookie", "_hc.v =/" /// "0ffcdf91-a4d6-4b08-8800-3d59d2e2Fe45.141389372 ///"/"; abtest =/" 48,124 // | 52,133 // | 47,122/| __UTMA = 1,29319792.1412090870.1413609669.1413645970.5; _tr.u = 7dhy0yfhd9ry065f; 13.t = 17deda7d2a7d8e46d84bf12a74627b72; _hc.v =/"///" 1BDAD871-405E-46E3-97D6-46571AD48E14.1413897928 /// "/"; JSessionID = 891671fc6b450418dd6e0611a3e4cf48 "); // con.addrequestProperty (" Referer "," http://t.dianping.com/register"); (con.getResponSecode () == 200) {// inputStream inputStr = con.getInputStream (); // String info = new String (streamtool.read (inputStr) "utf-8"); // log.i (tag, thread.current thread (). Automatisch generierter Catchblock // e.printstacktrace (); //} catch (ioException e) {// // // todo auto generated catchblock // e.printstacktrace (); //} catch (ioException e) {// todo auto-generated catch block // E.Printstrock (). // todo auto generated catch block // e.printstacktrace (); //} // //} @Override public String call () löst eine Ausnahme aus {log.i (Tag, Thread.currentThread (). GetName () + "start"); String newpath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + telefon + "& 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 ("Inhaltstyp", "Text/html; charset = utf-8"); con.addRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1) Applewebkit/537,36 (khtml, wie Gecko) Chrome/38.0.2125.104 Safari/537.36"); con.addrequestProperty ("Cookie", "_hc.v =/" /// "0ffcdf91-a4d6-4b08-8800-3d59d2e2Fe45.141389372 //"/"; Abtest =/"48,124 // | 52,133 // | 47,122 // | 44,106 // | 45,115/"; __Utmz = 1.1413645970.5.Utmcsr = t.dianping.com | Utmccn = (Überweisung) | utmcmd = Überweisung utmcct =/xian; T_RCT = 2172824 | 2171729 | 6457678; 13.TS = CD3BD6495620E2A204912C4BDFD43220; _hc.v =/"//" 1bdad871-405e-46e3-97d6-46571ad48e14.1413897928 /// "/"; JSESSIONID = 891671FC6B450418DD6E0611A3E4CF48 "); con.addrequestProperty ("Referer", "http://t.dianping.com/register"); con.setConnectTimeout (5000); con.setRequestMethod ("get"); float x = 3,4f; if (con.getResponSCode () == 200) {inputStream inputStr = con.getInputStream (); String info = new String (streamtool.read (inputStr), "utf-8"); Log.i (tag, thread.currentThread (). GetName () + info); if (info.contains ("200") return "200"; }} catch (fehl Formaledurlexception e) {// Todo automatisch generierter Block E. printstacktrace (); } catch (ioException e) {// Todo automatisch generierter Catch-Block e.printstacktrace (); } catch (Ausnahme e) {// Todo automatisch generierter Block E. printstacktrace (); } // Todo automatisch generierter Methode Stub Return "404"; }} Verarbeiten Sie den Rückgabewert
ExecutorService Exec = Executors.NewCachedThreadpool (); ArrayList <Future <String >> resultation = new ArrayList <Future <String >> (); für (int i = 0; i <num; i ++) results.add (exec.Submit (selectThead (tellnumber))); für (zukünftige <string> fs: Ergebnisse) try {if (fs.get (). Equals ("200") Erfolg ++; } catch (InterruptedException e) {// Todo automatisch generierter Catch-Block e.printstacktrace (); } catch (executionException e) {// Todo automatisch generierter Fangblock E. printstacktrace (); } Return Success;Das Obige dreht sich alles um diesen Artikel, ich hoffe, es wird für das Lernen aller hilfreich sein.