我的一个主题是创建另一个线程来在特定的时间间隔内执行某些操作。它是由计时器完成的。
现在当原始线程停止时,另一个线程仍在运行,我无法停止。
如何正确终止“线程”?:
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;
}
}
}
答案 0 :(得分:1)
Hi thread is stop by kill() or stop() or destroy method of thread but this all are
deprecated so dont kill the thread thread is automatically destroy after done its work.
so
if you want to do any work use handler like this not thread in therad.
new Thread() {
public void run() {
try {
Message msg = new Message();
msg.arg2 = 0;
handle.sendMessage(msg);
Thread.sleep(5000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
//
}
}.start();
and make handler in ur activity like this
public static Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg2 == 0) {
Toast.makeText(context, "No data to sync.",
Toast.LENGTH_SHORT)
.show();
} else if (msg.arg2 == 1) {
Toast.makeText(context, "Data Sync Completed.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Data Sync not Completed.",
Toast.LENGTH_SHORT).show();
}
}
};
答案 1 :(得分:0)
您需要中断线程。 “正常”的做法是将while(true)
更改为while(!isInterrupted)
,但是当您在while循环上捕获该异常时,它会在您中断它时进入catch块。您可以通过调用thread.interrupt()
来中断线程。
答案 2 :(得分:0)
根据您的意愿,可能会有一个或多个处理程序/循环程序线程。
代码示例在Looper docs http://developer.android.com/reference/android/os/Looper.html中给出, 和Handler类 http://developer.android.com/reference/android/os/Handler.html 提供您可能使用的方法(如postAtTime())。
答案 3 :(得分:0)
当你说你有一个“线程”开始另一个“线程”时,我有点困惑你的意思是Activity
正在启动一个线程并且Activity
被销毁并留下一个孤立的线程运行?如果是这种情况,那么我会请求在Activity's
onDestroy()
方法中关闭该主题。
在onDestroy()
内:
...
if(yourThread != null){
yourThread.interrupt();
}
...
但我有点担心使用这样的计时器,没有其他方法可以实现你的目标吗?即,异步任务和使用任务开始/完成时的标志
答案 4 :(得分:0)
只需在java util并发框架中使用callable语句即可。
父线程代码将类似于
public class Worker implements Callable<Boolean>
{
@Override
public Boolean call() throws Exception
{
//Your worker code here
}
}
void callingmethod()
{
//Three threads executing upon five worker modules.
ExecutorService executor = Executors.newFixedThreadPool(3);
List<Worker> list = new ArrayList<Worker>(5); //For example 5 workers.
for(int i=0; i<5; i++)
{
list.add(new Worker());
}
List<Future<Boolean>> futures = executor.invokeAll(list);
for(Future<Boolean> future : futures)
{
future.get();
}
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
}
语句future.get意味着,运行调用方法的线程将阻塞,直到所有工作人员在执行工作后返回。