了Thread.interrupt()不工作 [英] Thread.interrupt () doesn't work

查看:132
本文介绍了了Thread.interrupt()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海兰!

我的了Thread.interrupt不起作用。

code(我们是全球性的):

  //调用我们=新UpdateState(参数,可以HUP);
            us.start();// Interupt
@覆盖
    公共布尔onOptionsItemSelected(菜单项项){        如果(item.getItemId()== R.id.stopthread)
        {
            Log.e(杀,杀);
            us.interrupt();
        }
        返回super.onOptionsItemSelected(项目);
    }

类:

 包android.skiptvad;进口的java.util.List;进口org.apache.http.NameValuePair;进口android.os.Handler;
进口android.os.Message;
进口android.text.NoCopySpan.Concrete;
进口android.util.Log;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.util.Log;公共类UpdateState继承Thread {    公开名单<&的NameValuePair GT; PARAMS;
    公开处理程序处理程序;
    公开处理程序插件;    公共UpdateState(列表<&的NameValuePair GT;参数,可以最终处理程序处理){
        this.handler =处理程序;
        this.params =参数;
        this.ins =新的处理程序(){
            @覆盖
            公共无效的handleMessage(消息MSG){                如果(msg.obj.toString()!= NULL)
                {
                    JSONParse JSON = NULL;
                    尝试
                    {
                            消息MSG 2 =新的Message();
                            Log.e(Channel_State_Update,msg.obj.toString());
                            JSON =新JSONParse(msg.obj.toString());
                            字符串状态= json.getChannelState();
                            Log.e(Channel_State_Send状态);
                            msg2.obj =状态;
                            handler.sendMessage(MSG 2);                    }
                    赶上(最终例外五)
                    {
                            e.printStackTrace();
                    }
            }
        }
        };
    }    @覆盖
    公共无效的run(){            而(真)
            {
                如果(!this.isInterrupted())
                {
                    的HttpConnection CON =新的HttpConnection(参数,可以http://surfkid.redio.de/getChannelState,this.ins);
                    con.start();
                    尝试{
                        Log.e(睡眠,开始);
                        UpdateState.this.sleep(5000);
                        Log.e(睡眠,结束);
                    }赶上(InterruptedException的E){
                        // TODO自动生成catch块
                        Log.e(睡眠,错误);
                        e.printStackTrace();
                    }
                }
            //super.run();
        }
    }
}

请帮忙

日志:

  18 02-15:50:​​30.317:ERROR /睡眠(10696):结束
02-15 18:50:30.347:ERROR /睡眠(10696):开始
02-15 18:50:30.677:ERROR / Channel_State_Update(10696):{responseData:{channelState:0},responseDetails:空,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 /睡眠(10696):结束
02-15 18:50:35.357:ERROR /睡眠(10696):开始
02-15 18:50:35.897:ERROR / Channel_State_Update(10696):{responseData:{channelState:0},responseDetails:空,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 /杀(10696):杀死
02-15 18:50:36.878:ERROR /睡眠(10696):错误
02-15 18:50:36.908:ERROR /睡眠(10696):开始
02-15 18:50:37.427:ERROR / Channel_State_Update(10696):{responseData:{channelState:0},responseDetails:空,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 /睡眠(10696):结束
02-15 18:50:41.927:ERROR /睡眠(10696):开始


解决方案

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

  @覆盖
公共无效的run(){
    而(!this.isInterrupted()){//当线程的中断标志设置退出
        的HttpConnection CON =新的HttpConnection(参数,可以http://surfkid.redio.de/getChannelState,this.ins);
        con.start();
        尝试{
            Log.e(睡眠,开始);
            UpdateState.this.sleep(5000);
            Log.e(睡眠,结束);
        }赶上(InterruptedException的E){
            Log.e(睡眠,错误);
            e.printStackTrace();            //捕获InterruptedException的后恢复中断标志
            //使循环条件为假
            Thread.currentThread()中断()。
        }
    }
}

Hy!

My Thread.interrupt doesn't work.

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

解决方案

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();
        }
    }
}

这篇关于了Thread.interrupt()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆