如何从Java中的线程返回值? [英] How to return a value from thread in java?

查看:225
本文介绍了如何从Java中的线程返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android中,我正在创建用于url连接的线程.在线程中,我将响应消息存储在全局声明的字符串中.当我访问方法method时,它返回null.

In android i am creating thread for url connection.Inside the thread i store the response message in a string which is globally declared.When I access the method method it returns null.

    public class Rate_fetch {
       String total="";
          public String  rate(String dt)
       {
    new Thread(new Runnable(){ 

         public void run(){


            try  {

             URL url = new URL(tally_ipaddr+"/prorate.jsp?plist="+sss.toString().trim());

             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
             InputStream in = new BufferedInputStream(urlConnection.getInputStream());
             BufferedReader r = new BufferedReader(new InputStreamReader(in));
             String x = "";
             String total = "";
             x = r.readLine();
             int i=0;

             while(x.length()>1)
             {
                 total=total+x.toString().trim();
                 i++;
                 x = r.readLine();
             }
             }
            catch(Exception e){
             return e.toString();
            }   
        }    

    }).start();

    return total;



}

当我调用该方法时,它返回null.

When i call the method it returns null.

 Rate_fetch rf=new Rate_fetch();
      String amt= rf.rate(prodList);

推荐答案

如果您只想使用Thread,请尝试

It you really want to use Thread only, try this

    public class Rate_fetch {
    String total = "";
    boolean b = true;

    public String rate(String dt) {
        StringBuilder sb = new StringBuilder();
        new Thread(new Runnable() {

            public void run() {

                try {

                    URL url = new URL(tally_ipaddr + "/prorate.jsp?plist="
                            + sss.toString().trim());

                    HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();
                    InputStream in = new BufferedInputStream(urlConnection
                            .getInputStream());
                    BufferedReader r = new BufferedReader(
                            new InputStreamReader(in));
                    StringBuilder sb = new StringBuilder();
                    String s;
                    while (true) {
                        s = r.readLine();
                        if (s == null || s.length() == 0)
                            break;
                        sb.append(s);
                    }
                    b = true;
                } catch (Exception e) {
                    b = true;
                }
            }

        }).start();
        while (b) {

        }
        total = sb.toString();
        return sb.toString();

    }
}

这篇关于如何从Java中的线程返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆