服务器之家

服务器之家 > 正文

java线程的run()没有返回值怎么办?

时间:2020-03-20 13:51     来源/作者:学习编程知识

用线程Thread执行一些方法后,需要判断执行是否成功。

public void run() {}

run( ) 方法返回值是空, 怎么办?

解决方法:

  • Note
  • 使用 call() 方法
  • Callable接口是 jdk 5 后新增的接口

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
 
import android.util.Log;
 
import com.example.StreamTool.StreamTool;
 
public class MyThread1 implements Callable<String> {
  private static final String tag = "xxxyyy";
  private String phone;
  private String name;
  public MyThread1(String name, String phone) {
 
    this.name = name;
    this.phone = phone;
  }
 
// public MyThread1(String name, String phone) {
//   super(name);
//   this.phone = phone;
//  }
 
//  public void run() {
//   Log.i(tag, Thread.currentThread().getName() + "......start");
//   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);
//
//   try {
//     URL url = new URL(strBui.toString());
//     HttpURLConnection con = (HttpURLConnection) url.openConnection();
//     con.addRequestProperty("Content-Type", "text/html; charset=UTF-8");
//     con.addRequestProperty(
//             "User-Agent",
//             "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like 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\\|47,122\\|44,106\\|45,115\"; __utma=1.29319792.1412090870.1413609669.1413645970.5; __utmz=1.1413645970.5.2.utmcsr=t.dianping.com|utmccn=(referral)|utmcmd=referral|utmcct=/xian; _tr.u=7dhY0yFhd9rY065F; tc=17; cy=17; cye=xian; t_rct=2172824|2171729|6457678; PHOENIX_ID=0a01743f-1492e1321c8-46e6a; 13.t=17deda7d2a7d8e46d84bf12a74627b72; 13.ts=cd3bd6495620e2a204912c4bdfd43220; thirdtoken=891671FC6B450418DD6E0611A3E4CF48; _hc.v=\"\\\"1bdad871-405e-46e3-97d6-46571ad48e14.1413897928\\\"\"; _tr.s=Zy1Ca2OAe1ptv06o; JSESSIONID=891671FC6B450418DD6E0611A3E4CF48");
//     con.addRequestProperty("Referer", "http://t.dianping.com/register");
//     con.setConnectTimeout(5000);
//     con.setRequestMethod("GET");
//    
//     if (con.getResponseCode() == 200){
//       InputStream inputStr = con.getInputStream();
//       String info = new String(StreamTool.read(inputStr), "UTF-8");
//       Log.i(tag, Thread.currentThread().getName() + info);
//     }
//   } catch (MalformedURLException e) {
//     // TODO Auto-generated catch block
//     e.printStackTrace();
//   } catch (IOException e) {
//     // TODO Auto-generated catch block
//     e.printStackTrace();
//   } catch (Exception e) {
//     // TODO Auto-generated catch block
//     e.printStackTrace();
//   }
//  
//   
//  }
 
  @Override
  public String call() throws Exception {
      Log.i(tag, Thread.currentThread().getName() + "......start");
      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);
 
      try {
        URL url = new URL(strBui.toString());
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.addRequestProperty("Content-Type", "text/html; charset=UTF-8");
        con.addRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like 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\\|47,122\\|44,106\\|45,115\"; __utma=1.29319792.1412090870.1413609669.1413645970.5; __utmz=1.1413645970.5.2.utmcsr=t.dianping.com|utmccn=(referral)|utmcmd=referral|utmcct=/xian; _tr.u=7dhY0yFhd9rY065F; tc=17; cy=17; cye=xian; t_rct=2172824|2171729|6457678; PHOENIX_ID=0a01743f-1492e1321c8-46e6a; 13.t=17deda7d2a7d8e46d84bf12a74627b72; 13.ts=cd3bd6495620e2a204912c4bdfd43220; thirdtoken=891671FC6B450418DD6E0611A3E4CF48; _hc.v=\"\\\"1bdad871-405e-46e3-97d6-46571ad48e14.1413897928\\\"\"; _tr.s=Zy1Ca2OAe1ptv06o; JSESSIONID=891671FC6B450418DD6E0611A3E4CF48");
        con.addRequestProperty("Referer", "http://t.dianping.com/register");
        con.setConnectTimeout(5000);
        con.setRequestMethod("GET");
 
        float x = 3.4f;
        if (con.getResponseCode() == 200){
          InputStream inputStr = con.getInputStream();
          String info = new String(StreamTool.read(inputStr), "UTF-8");
          Log.i(tag, Thread.currentThread().getName() + info);
          if(info.contains("200"))
            return "200";
        }
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
 
 
    // TODO Auto-generated method stub
    return "404";
  }
 
 
}

处理返回值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ExecutorService exec = Executors.newCachedThreadPool();
    ArrayList<Future<String>> results = new ArrayList<Future<String>>();
    for(int i = 0; i < num; i++)
      results.add(exec.submit(selectThread(telnumber)));
    for (Future<String> fs:results)
      try {
        if (fs.get().equals("200"))
          success++;
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    return success;

以上就是本文的全部内容,希望对大家的学习有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部