java Thread.interrupt() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5008176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:02:42  来源:igfitidea点击:

Thread.interrupt () doesn't work

javaandroid

提问by user547995

Hy!

嗨!

My Thread.interrupt doesn't work.

我的 Thread.interrupt 不起作用。

Code (us is global):

代码(我们是全球的):

//Call

us = new UpdateState(params, hup);
            us.start();

//Interupt
@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId()== R.id.stopthread)
        {
            Log.e("Kill", "Kill");
            us.interrupt();
        }
        return super.onOptionsItemSelected(item);
    }

Class:

班级:

package android.skiptvad;

import java.util.List;

import org.apache.http.NameValuePair;

import android.os.Handler;
import android.os.Message;
import android.text.NoCopySpan.Concrete;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class UpdateState extends Thread {

    public List<NameValuePair> params;
    public Handler handler;
    public Handler ins;

    public UpdateState(List<NameValuePair> params, final Handler handler) {
        this.handler = handler;
        this.params = params;
        this.ins = new Handler (){
            @Override
            public void handleMessage(Message msg) {

                if (msg.obj.toString()!= null)
                {
                    JSONParse json = null;
                    try 
                    {       
                            Message msg2 =new Message();
                            Log.e("Channel_State_Update",msg.obj.toString());
                            json = new JSONParse(msg.obj.toString());
                            String state = json.getChannelState();
                            Log.e("Channel_State_Send",state);
                            msg2.obj = state;
                            handler.sendMessage(msg2);

                    } 
                    catch (final Exception e) 
                    {
                            e.printStackTrace();
                    }
            }
        }
        };
    }

    @Override
    public void run() {

            while (true)
            {
                if (!this.isInterrupted())
                {
                    HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/getChannelState", this.ins);
                    con.start();
                    try {
                        Log.e("Sleep", "Begin");
                        UpdateState.this.sleep(5000);
                        Log.e("Sleep", "End");
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("Sleep", "Error");
                        e.printStackTrace();
                    }
                }




            //super.run();
        }
    }
}

Please help

请帮忙

Log:

日志:

02-15 18:50:30.317: ERROR/Sleep(10696): End
02-15 18:50:30.347: ERROR/Sleep(10696): Begin
02-15 18:50:30.677: ERROR/Channel_State_Update(10696): {"responseData":{"channelState":"0"},"responseDetails":null,"responseStatus":200}
02-15 18:50:30.677: ERROR/Channel_State_Send(10696): 0
02-15 18:50:30.677: ERROR/UPDATE(10696): 0
02-15 18:50:35.347: ERROR/Sleep(10696): End
02-15 18:50:35.357: ERROR/Sleep(10696): Begin
02-15 18:50:35.897: ERROR/Channel_State_Update(10696): {"responseData":{"channelState":"0"},"responseDetails":null,"responseStatus":200}
02-15 18:50:35.897: ERROR/Channel_State_Send(10696): 0
02-15 18:50:35.897: ERROR/UPDATE(10696): 0
02-15 18:50:36.868: ERROR/Kill(10696): Kill
02-15 18:50:36.878: ERROR/Sleep(10696): Error
02-15 18:50:36.908: ERROR/Sleep(10696): Begin
02-15 18:50:37.427: ERROR/Channel_State_Update(10696): {"responseData":{"channelState":"0"},"responseDetails":null,"responseStatus":200}
02-15 18:50:37.427: ERROR/Channel_State_Send(10696): 0
02-15 18:50:37.427: ERROR/UPDATE(10696): 0
02-15 18:50:41.909: ERROR/Sleep(10696): End
02-15 18:50:41.927: ERROR/Sleep(10696): Begin

回答by axtavt

You need to break a loop when thread is interrupted:

当线程被中断时,您需要中断一个循环:

@Override
public void run() {
    while (!this.isInterrupted()) { // Exit when thread's interrupt flag is set
        HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/getChannelState", this.ins);
        con.start();
        try {
            Log.e("Sleep", "Begin");
            UpdateState.this.sleep(5000);
            Log.e("Sleep", "End");
        } catch (InterruptedException e) {
            Log.e("Sleep", "Error");
            e.printStackTrace();

            // Restore interrupt flag after catching InterruptedException
            // to make loop condition false
            Thread.currentThread().interrupt();
        }
    }
}

回答by Chris Dennett

What do you expect to happen? You have the interrupt exception in a massive while loop :) You need to write break; inside the exception part for it to exit the loop. The if !this.isInterrupted()line only makes the loop extremely tight by eliminating the code inside, but the loop is still there on the outside.

你期望发生什么?你在大量的 while 循环中有中断异常:) 你需要写 break; 在异常部分内使其退出循环。该if !this.isInterrupted()行仅通过消除内部代码使循环变得非常紧密,但循环仍然存在于外部。