HY!
My Thread.interrupt不起作用。
代码(我们是全球性的):
//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);
}
类别:
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();
}
}
}
请帮忙
日志:
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
答案 0 :(得分:9)
线程中断时需要断开循环:
@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();
}
}
}
答案 1 :(得分:1)
您期望发生什么?在大量的while循环中你有中断异常:)你需要写中断;在异常部分内部,它退出循环。 if !this.isInterrupted()
行仅通过消除内部代码使循环非常紧,但循环仍在外部。