私のスレッドの 1 つは、特定の間隔で何かを行うために別のスレッドを作成しています。これはタイマーによって行われます。
元のスレッドが停止しても、他のスレッドはまだ実行中であり、停止させることはできません。
「スレッド」を適切に終了するにはどうすればよいですか?:
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;
}
}
}