How multi-Threading is implemented in the business code (Java Class) and returns the value of the child thread with the method

Source: Internet
Author: User

Reprinted from http://bbs.csdn.net/topics/390731832

Problem:

 Public StaticString getaddress (FinalInputStream InputStream,FinalString Mobile) {        NewThread () { Public voidrun () {Try{log.i (TAG,"InputStream:" +inputstream.available ()); String Soap=Readsoapfile (InputStream, mobile); byte[] data =soap.getbytes (); URL URL=NewURL ("Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setdooutput (true);//set Allow outputConn.setconnecttimeout (5 * 1000);//set the time-out to 5 secondsConn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type",                            "Application/soap+xml; Charset=utf-8 "); Conn.setrequestproperty ("Content-length", String.valueof (data.length)); //Set LengthOutputStream OutputStream=Conn.getoutputstream ();                    Outputstream.write (data);                    Outputstream.flush ();                     Outputstream.close (); if(Conn.getresponsecode () = = 200) {Address=Parseresponsexml (Conn. getInputStream ());//parse the data returned by the server side}System.out.println ("address==" +address);//here is a value                    }                } Catch(Exception e) {log.e (TAG, e.tostring ()); System.out.println ("Get Phone number attribution failed");        }            };        }.start (); System.out.println ("Addres" +address);//The return here is empty.        returnaddress; }

addess return null value

Because it is not in a thread, the value cannot be passed across threads, the workaround:

 Public StaticString getaddress (FinalInputStream InputStream,FinalString Mobile) {Thread Thread=NewThread () { Public voidrun () {Try{Log. I (TAG,"InputStream:" +inputstream.available ()); String Soap=Readsoapfile (InputStream, mobile); byte[] data =soap.getbytes (); URL URL=NewURL ("Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" ); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setdooutput (true); Conn.setconnecttimeout (5 * 1000); Conn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type",                                           "Application/soap+xml; Charset=utf-8 "); Conn.setrequestproperty ("Content-length", String. ValueOf (data.length)); OutputStream OutputStream=Conn.getoutputstream ();                              Outputstream.write (data);                              Outputstream.flush ();                                                              Outputstream.close (); if(Conn.getresponsecode () = = 200) {Address=Parseresponsexml (Conn. getInputStream ()); }                        } Catch(Exception e) {}};            };             Thread.Start (); try {thread.join ();} catch (Exception e) {} if (address!=null) {return address; } return NULL ; }

How multi-Threading is implemented in the business code (Java Class) and returns the value of the child thread with the method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.