惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 大力

删除SQL架构的用户 支付宝 公钥 应用公钥 apidoc FlexCel 插入公式和插入新行 DataTable 修改列名 删除列 调整列顺序 SQL Server CTE 递归查询全解 HTML空格占位符 如何:在 ASP.NET 网页中检测浏览器类型 VS格式化代码 Newtonsoft.Json Dell DRAC的重启方法 iTunes Web.Config中的<compilation debug="true"/> C#转VB.NET Java线程中带有返回值的线程Callable Android 子线程测试 JAVA下的Thread.sleep方法一定要try 解决Android Studio 和 Android SDK Manager 无法在线更新的问题. asp.net关于页面不回发,不生成__doPostBack方法问题的完美解决方案
thread.join 从异步执行变成同步
大力 · 2015-05-17 · via 博客园 - 大力
  •    Java的线程模型为我们提供了更好的解决方案,这就是join方法。在前面已经讨论过,join的功能就是使用线程 从异步执行变成同步执行
  •   当线程变成同步执行后,就和从普通的方法中得到返回数据没有什么区别了。因此,可以使用如下的代码更有效地解决这个问题:
  •   
  • Java代码       
  • thread.start();    
  • thread.join();    
  • ...    
  •  在thread.join()执行完后,线程thread的run方法已经退出了,也就是说线程thread已经结束了。因此,在thread.join()后面可以放心大胆地使用MyThread类的任何资源来得到返回数据。   

public static String getAddress (final InputStream inputStream, final String mobile) {
            Thread thread = new Thread() {
                   public void run() {
                         try {
                              Log. i(TAG, "inputStream: " + inputStream.available());
                              String soap = readSoapFile(inputStream, mobile);
                               byte[] data = soap.getBytes();

                              URL url = new URL(
                                           "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 ;
      }

posted @ 2015-05-17 14:12  大力  阅读(844)  评论()    收藏  举报