程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java線程的run()沒有前往值怎樣辦?

java線程的run()沒有前往值怎樣辦?

編輯:關於JAVA

java線程的run()沒有前往值怎樣辦?。本站提示廣大學習愛好者:(java線程的run()沒有前往值怎樣辦?)文章只能為提供參考,不一定能成為您想要的結果。以下是java線程的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;

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved