Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

java - Thread.interrupt () doesn't work

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...