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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 大力

删除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)  评论()    收藏  举报