欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-08 08:57:04
用线程thread执行一些方法后,需要判断执行是否成功。 public void run() {} run( ) 方法返回值是空, 怎么办? 解决方法:...

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

public void run() {}

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

解决方法:

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

代码:

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";
  } 


}

处理返回值

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;

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