Android:Thread.sleep不';t端

Android:Thread.sleep不';t端,android,multithreading,sleep,Android,Multithreading,Sleep,我有一个带有httprequest的应用程序。我想为该请求设置一个超时,由于我无法控制de DNS超时,stackoverflow的一个用户建议创建一个带有计时器的线程,以在特定时间后取消该请求,这就是我正在做的 在我的新线程中,我使用Thread.sleep等待30秒,当时间结束时,如果请求尚未结束,则取消请求,但我的线程从未唤醒。这是我的代码: private Thread timeout_request = new Thread(new Runnable() { public vo

我有一个带有httprequest的应用程序。我想为该请求设置一个超时,由于我无法控制de DNS超时,stackoverflow的一个用户建议创建一个带有计时器的线程,以在特定时间后取消该请求,这就是我正在做的

在我的新线程中,我使用Thread.sleep等待30秒,当时间结束时,如果请求尚未结束,则取消请求,但我的线程从未唤醒。这是我的代码:

private Thread timeout_request = new Thread(new Runnable() {
    public void run() {
        try{
            Log.d(TAG, "Thread start");
            Thread.sleep(30000);
            Log.d(TAG, "Thread awake");
            if(!httprequest_finished){
                request_aborted = true;
                mHttpClient.getConnectionManager().shutdown();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
});

public String sendData(String token){
    ...
    timeout_request.start();
    Log.i(TAG, "Sending request");
    final HttpResponse resp = httpClient.execute(post);
    Log.i(TAG, "Execute finished");
    if(request_aborted) return null;
    httprequest_finished = true;
    ...
}
如果我在没有internet连接的情况下执行sendData函数,日志会显示“发送请求”和“线程启动”,但不会显示“线程唤醒”或“执行完成”(我已经等了5分钟)。为什么呢

------编辑------

顺便说一下,我不知道这是否重要,但我正在连接中使用ThreadSafeClientConnManager

我已经测试了一些东西:

1-将HttpResponse resp=httpClient.execute(post)替换为while(!request_中止)。线程唤醒(它工作)

2-使用mHandler.postDelayed(r,30000)代替线程。我仍然在执行httpClient.execute(post)。结果:未启动Runnable

private Handler mHandler = new Handler();
private Runnable r = new Runnable() {
    public void run() {
        Log.d(TAG, "Runnable()");
        if(!httprequest_finished){
            request_aborted = true;
            mHttpClient.getConnectionManager().shutdown();
        }
    }
};
3-使用mHandler.postDelayed(r,30000)代替线程。将HttpResponse resp=httpClient.execute(post)替换为while(!request_aborted)。结果:未启动Runnable

private Handler mHandler = new Handler();
private Runnable r = new Runnable() {
    public void run() {
        Log.d(TAG, "Runnable()");
        if(!httprequest_finished){
            request_aborted = true;
            mHttpClient.getConnectionManager().shutdown();
        }
    }
};
因此,如果我没有执行http请求,线程就会工作,但处理程序永远不会工作。我在日志中注意到以下几行:

threadid=3: reacting to signal 3
Wrote stack traces to '/data/anr/traces.txt'
threadid=3: reacting to signal 3
Wrote stack traces to '/data/anr/traces.txt'
这在执行线程或设置处理程序6或7秒后出现。我尝试使用:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
            Log.e("Alert","Lets See if it Works !!!");
        }
    });

但是日志中从来没有显示“让我们看看它是否有效!!!”。我查看了traces.txt文件,不明白它在说什么-。-

我已经测试了以下代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author paro
 */
public class JavaApplication1 {

    public boolean req_aborted = false;
    public Thread timeout_request = new Thread(new Runnable() {
    public void run() {
        try{
            System.out.println("start");
            Thread.sleep(30000);
            System.out.println("awake");
            if(req_aborted ==false)
            {
                req_aborted = true;
                System.out.println("flag change");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
});
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JavaApplication1 jap = new JavaApplication1();
        jap.timeout_request.start();
        System.out.println("requset started");
        if(jap.req_aborted)
        {
            System.out.println("flag is true now");
        }
    }
}
输出是

requset started
start
awake
flag change
代码中与线程相关的部分似乎没有问题

您的代码的逻辑表明它应该打印“awake”。似乎您对以下陈述有疑问

final HttpResponse resp = httpClient.execute(post);

我认为应该使用
Handler.postDelayed
,而不是使用
Thread.sleep
。看一看这张照片

比如:

Handler mHandler = new Handler();
...
mhandler.postDelayed(new Runnable() {
    public void run() {
        if(!httprequest_finished){
            request_aborted = true;
            mHttpClient.getConnectionManager().shutdown();
        }
    }
}, 30000);

这将在指定的延迟后执行传入的
Runnable

不能使用“HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),10000);//超时限制”,这对DNS解析不起作用。我能想到的唯一一件事是请求(httpClient.execute(post))使用了所有的CPU,我的线程没有执行,但这意味着Android的设计真的很糟糕(除非有改变它的选项)。我认为post请求不太可能消耗掉所有的CPU,但也许这只是我,Android HHTP确实是。。。不好,是的,我觉得不太可能。。。我会继续尝试的。