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 onOptionsIte
Solution 1:
You need to break a loop when thread is interrupted:
@Override
publicvoidrun() {
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();
}
}
}
Solution 2:
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.
Post a Comment for "Thread.interrupt () Doesn't Work"