我的一个线程正在创建另一个线程以在特定时间间隔内执行某些操作。它是通过计时器来完成的。
现在当原始线程停止时,另一个线程仍在运行,我无法让它停止。
如何正确终止“线程”?:
Thread thread = new Thread() {
@Override
public void run() {
try {
while (true) {
sleep(1000);
// do something
}
} catch (InterruptedException e) {
Log.e(TAG,"new invoked caught the exception!");
return;
}
}
};
public void run() {
while (true) {
try {
Log.d(TAG,"ConnectedThread.run: Reading from InStream socket");
while (true) {
thread.start();
Log.e(TAG,"starting additional thread");
// Read from the InputStream
int inB;
inB = mmInStream.read();
if (inB != -1) mAccessoryInfo.addCharacter((byte) inB);
}
} catch (IOException e) {
Log.e(TAG, "TADA InStream read exception, disconnected "
+ e.getMessage());
// stopService(new Intent(getBaseContext(), Pinger.class));
thread = null;
connectionLost();
break;
}
}
}