Setelah menjalankan beberapa metode dengan utas utas, Anda perlu menentukan apakah eksekusi berhasil.
public void run () {}
Nilai pengembalian metode run () kosong, apa yang harus saya lakukan?
Larutan:
Kode:
Paket com.example.thread; import java.io.ioException; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; impor java.util.concurrent.callable; com.example.streamTool.streamTool; kelas publik MyThread1 mengimplementasikan Callable <string> {private static final string tag = "xxxyyy"; telepon string pribadi; nama string pribadi; public mythread1 (nama string, string phone) {this.name = name; this.phone = telepon; } // public mythread1 (nama string, string phone) {// super (name); // this.phone = phone; //} // public void run () {// log.i (tag, thread.currentThread (). getName () + " "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 = new Url (strbui.tostring ()); // httpurlconnection con = (httpurlconnection) url.openconnection (); // con.addrequestproperty ("content-type", "text/html; charset = UTF-8"); // CONTE.ADDRE "" "Mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (khtml, seperti tokek) 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 // 48.124 // | __utma = 1.29319792.1412090870.1413609669.1413645970.5; _tr.u = 7dhy0yfhd9ry065f; 13.T = 17DIDA7D2A7D8E46D84BF12A74627B72; _HC.V =/"///" 1BDAD871-405E-46E3-97D6-46571AD48E14.1413897928 /// "/"; JSessionId = 891671FC6B450418DD6E0611A3E4CF48 "); // CON.ADDREQUESTPROPERTY (" Referensi "," http://t.dianping.com/register");// con.setconnecttimeout (5000);///conetrut ("(5000); (con.getResponsecode () == 200) {// inputStream inputStr = con.getInputStream (); // string info = string baru (streamTool.read (inputstr), "UTF-8"); // log.i (tag, thread.currentThread (). GetName () + Info); // {{} {} {{{{). TODO TODO AUTO-EKSIRATED CATCH Block // E.PrintStackTrace (); //} Catch (ioException e) {// // // TODO AUTO-AUTO-ENTEERATED Catch Block // E.PrintStackTrace (); // {IoException E) {// // TODO TODO Auto-Generated Catch Block/E. // TODO AUTO-AUTO-ENTEERATED CATCH Block // E.PrintStackTrace (); //} // //} @Override Public String call () melempar Exception {log.i (tag, thread.currentThread (). GetName () + "...... start"); String newPath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + telepon + "& flow = t & callback = dp._jsonprequest._5"; StringBuilder strbui = StringBuilder baru (newPath); coba {url url = url baru (strbui.toString ()); HttpurlConnection con = (httpurlconnection) url.openconnection (); con.addrequestproperty ("tipe konten", "teks/html; charset = utf-8"); con.addrequestproperty ("agen-pengguna", "mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (khtml, seperti tokek) 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. 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.getResponsecode () == 200) {inputStream inputstr = con.getInputStream (); String info = string baru (streamTool.read (inputStr), "UTF-8"); Log.i (tag, thread.currentThread (). GetName () + info); if (info.contains ("200")) mengembalikan "200"; }} catch (MalFormedurlexception e) {// TODO Auto-Encanerated Catch Block E.PrintStackTrace (); } catch (ioException e) {// TODO AUTO-ENCEALATED Catch Block E.PrintStackTrace (); } catch (Exception e) {// TODO Auto-Entoerated Catch Block E.PrintStackTrace (); } // TODO METODE AUTO-AUTO-METODE RETTOK "404"; }} Proses Nilai Pengembalian
ExecutorService exec = executors.newcachedThreadpool (); ArrayList <Future <string>> result = new ArrayList <Future <String> (); untuk (int i = 0; i <num; i ++) results.add (exec.submit (selectthread (telnumber)))); untuk (masa depan <string> fs: hasil) coba {if (fs.get (). equals ("200")) SUCCESS ++; } catch (InterruptedException E) {// TODO Auto-Encanerated Catch Block E.PrintStackTrace (); } catch (ExecutionException E) {// TODO Auto-Enchyated Catch Block E.PrintStackTrace (); } mengembalikan kesuksesan;Di atas adalah semua tentang artikel ini, saya harap ini akan membantu untuk pembelajaran semua orang.