Depois de executar alguns métodos com threads, você precisa determinar se a execução é bem -sucedida.
public void run () {}
O valor de retorno do método run () está vazio, o que devo fazer?
Solução:
Código:
pacote com.example.Thread; importar java.io.ioException; importar java.io.inputStream; importar java.net.httpurlConnection; importar java.net.malformedurlexception; import java.net.url; import java.util.concurrent.callable; com.example.streamtool.streamtool; public class mythread1 implementa Callable <String> {private Static final String tag = "xxxyyy"; telefone de corda privada; nome de string privado; public mythread1 (nome da string, string telefone) {this.name = name; this.phone = telefone; } // public mythread1 (nome da string, string telefone) {// super (nome); // this.phone = telefone; //} // public void run () {// log.i (tag, thread.currentThread (). getName () + "...... start"); "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + telefone + "& Flow = t & callback = dp._jsonprequest._5"; // stringbuilder strbui = new stringbuilder (newpath); // {////5; Url (strbui.toString ()); // httpurlConnection con = (httpurlConnection) url.openconnection (); // con.addrequestProperty ("content-type", "text/html; charset = utf-8") //k.adrequim ", "Mozilla/5.0 (Windows NT 6.1) Applewebkit/537.36 (Khtml, como Gecko) Chrome/38.0.2125.104 Safari/537.36"); "_hc.v =/" // "0FFCDF91-A4D6-4B08-8800-3D59D2E2FE45.1413899372 //"/"; ABTEST =/" 48,124 // | 52,133 // | 47,122222 //; __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 (" referencial "," 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.curnthread (). Bloco de captura de captura automática // e.rintStackTrace (); //} catch (ioexception e) {// // // TODO Auto-Generated Catch Block // e.printStackTrace (); //} Catch (ioexception e) {// // TODO-Generated Catch Block/ E.PraceDO (ioException e) {// // TODO-Generated Catch Block/ E. Bloco de captura gerado automaticamente // e.printStackTrace (); //} // //} @Override public String Call () lança exceção {log.i (tag, thread.currentThread (). GetName () + "...... start"); String newpath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + telefone + "& fluxo = t & chamado de chamada = dp._jsonprequest._5"; Stringbuilder strbui = new StringBuilder (newPath); tente {url url = novo url (strbui.toString ()); HttpurlConnection con = (httpurlConnection) url.openconnection (); con.addrequestProperty ("conteúdo-tipo", "text/html; charset = utf-8"); Con.AddRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1) Applewebkit/537.36 (KHTML, como Gecko) Chrome/38.0.2125.104 Safari/537.36"); con.addrequestProperty ("cookie", "_hc.v =/" /// "0FFCDF91-A4D6-4B08-8800-3D59D2E2FE45.1413899372 ///"/"; abtest =/"48,124 // | 52,133 // | 47,122 // | 44,106 // | 45,115/"; __utmz = 1.1413645970.5.2.utmcsr = t.dianping.com | T_RCT = 2172824 | 2171729 | 6457678; 13.TS = CD3BD6495620E2A204912C4BDFD43220; _hc.v =/"//" 1bdad871-405e-46e3-97d6-46571ad48e14.1413897928 // "/"; JSessionID = 891671FC6B450418DD6E0611A3E4CF48 "); con.addrequestProperty ("referente", "http://t.dianping.com/register"); Con.SetConnectTimeout (5000); con.setRequestMethod ("get"); flutuar x = 3,4f; if (con.getResponsecode () == 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") retornar "200"; }} catch (malformedUrlexception e) {// TODO BLOCO DE CATAGEM AUTOGERATION E.PRINTSTACKTRACE (); } catch (ioexception e) {// TODO GATO GENERADO AUTOMENTADO BLOCO E.PRINTSTACKTRACE (); } Catch (Exceção e) {// TODO BLOCO DE CAPAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } // TODO Method Auto-Gerated Stub Return "404"; }} Processe o valor de retorno
ExecutorService Exec = executores.newcachedthreadpool (); ArrayList <FUTTURA <String>> Resultados = new ArrayList <FUTTURA <String>> (); for (int i = 0; i <num; i ++) resultados.add (Exec.submit (selectThread (telNumber))); para (futuro <string> fs: resultados) tente {if (fs.get (). Equals ("200")) Sucesso ++; } catch (interruptedException e) {// TODO BLOCO DE CATCH AUTOGERATIDO E.PRINTSTACKTRACE (); } catch (ExecutionException e) {// TODO BLOCO DE CAPAGEM AUTOGERATO E.PRINTSTACKTRACE (); } retornar sucesso;O exposto acima é tudo sobre este artigo, espero que seja útil para o aprendizado de todos.