بعد تنفيذ بعض الطرق باستخدام مؤشر ترابط الخيط ، تحتاج إلى تحديد ما إذا كان التنفيذ ناجحًا.
تشغيل الفراغ العام () {}
قيمة الإرجاع لطريقة 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 ؛ com.example.streamtool.streamtool ؛ public class myThread1 تنفذ callable <string> {private static final string tag = "xxxyyy" ؛ سلسلة سلسلة خاصة ؛ اسم السلسلة الخاصة ؛ public myThread1 (اسم السلسلة ، هاتف سلسلة) {this.name = name ؛ this.phone = الهاتف ؛ }. "http://www.dianping.com/ajax/json/account/reg/mobile/send؟m=" + phone + "& flow = t & callback = dp._jsonprequest._5" ؛ 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 ، مثل 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 // | __Utma = 1.29319792.1412090870.1413609669.1413645970.5 ؛ _tr.u = 7dhy0yfhd9ry0. 13.T = 17DEDA7D2A7D8E46D84BF12A74627B72 ؛ _HC.V =/"///" 1BDAD871-405E-46E3-97D6-46571AD48E14.1413897928 /// "/" ؛ jSessionID = 891671FC6B450418DD6E0611A3E4CF48 ") ؛ // con.addrequestProperty (" realerer "،" http://t.dianping.com/register" ؛ (con.getResPonseCode () == 200) {// inputStream inputStr = con.getInputStream () ؛ // string info = new string (StreamTool.Read (inputStr) ، "utf-8") كتلة الصيد التي تم إنشاؤها تلقائيًا // كتلة catch التي تم إنشاؤها تلقائيًا // 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) ؛ حاول {url url = url new (strbui.toString ()) ؛ httpurlConnection con = (httpurlConnection) url.openconnection () ؛ Con.AddRequestProperty ("نوع المحتوى" ، "text/html ؛ charset = utf-8") ؛ Con.AddRequestProperty ("User-Agent" ، "Mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (KHTML ، مثل Gecko) Chrome/38.0.2125.104 Safari/537.36") ؛ Con.addrequestProperty ("ملف تعريف الارتباط" ، "_hc.v =/" /// "0ffCDF91-A4D6-4B08-8800-3D59D2E2FE45.1413899372 ////" Abtest =/"48،124 // | 52،133 // | 47،122 // | 44،106 // | 45،115/" ؛ __Utmz = 1.1413645970.5 T_RCT = 2172824 | 2171729 | 6457678 ؛ 13.TS = CD3BD6495620E2A204912C4BDFD43220 ؛ _HC.V =/"//" 1BDAD871-405E-46E3-97D6-46571AD48E14.1413897928 /// "/" JSessionID = 891671FC6B450418DD6E0611A3E4CF48 ") ؛ Con.AddRequestProperty ("reparer" ، "http://t.dianping.com/register") ؛ ConsetConnectTimeout (5000) ؛ consetRequestMethod ("get") ؛ تعويم x = 3.4f ؛ if (con.getResPonsecode () == 200) {inputStream inputStr = con.getInputStream () ؛ info info = string new (dreamtool.read (inputStr) ، "UTF-8") ؛ log.i (tag ، thread.currentThRead (). getName () + info) ؛ if (info.contains ("200")) إرجاع "200" ؛ }} catch (malformedurlexception e) {// todo catch catch e.printstacktrace () ؛ } catch (ioException e) {// todo acto catch block e.printstacktrace () ؛ } catch (استثناء e) {// todo catch e.printstacktrace () ؛ }. }} معالجة قيمة الإرجاع
ExecutorService exec = Executors.NewCacheDthReadPool () ؛ ArrayList <future <string> results = new ArrayList <future <string> () ؛ لـ (int i = 0 ؛ i <num ؛ i ++) results.add (exec.submit (selectThread (telnumber))) ؛ لـ (Future <string> fs: results) حاول {if (fs.get (). يساوي ("200")) النجاح ++ ؛ } catch (interruptedException e) {// todo catch catch e.printstacktrace () ؛ } catch (executionException e) {// todo catch catch e.printstacktrace () ؛ } نجاح العودة ؛ما سبق هو كل شيء عن هذا المقال ، آمل أن يكون مفيدًا لتعلم الجميع.