简体   繁体   English

Android取消线程

[英]Android cancel Thread

I am doing a simple Async operation with Android, but since I want to execute the action couple of times, I am not using AsyncTask, I instead use Thread/Runnable mechanism with Handler to handle messages and staff. 我正在用Android做一个简单的Async操作,但是由于我想执行几次动作,所以我不使用AsyncTask,而是使用Thread / Runnable机制和Handler处理消息和人员。 But at one point when I need to execute the second operation, I need to cancel the previous operation if it is still active. 但是在需要执行第二个操作的某个时刻,如果前一个操作仍处于活动状态,则需要取消它。

I have something like this: 我有这样的事情:

private void exec() {
    new Thread(new Runnable() {
    public void run() {
    mBind.exec(3);
    }
  }).start();
}

Then in exec(int a) I have an interation like: 然后在exec(int a)中,我有一个类似的问题:

for(int i = 0; i<=res.lenght; i++) {
   updateGui();
}

But at one point the exec() method is called for second time, and the gui is updated with the previous results too (I need only the results from the new (2nd) request). 但是有一次第二次调用exec()方法,并且gui也​​使用以前的结果进行了更新(我只需要来自新(第二个)请求的结果)。

I know there is way to do this with FutureTask and play with cancel() or with Thread's 'throw ThreadDead' exception, but I am just curious if I can do it the same way I started in the first place. 我知道有办法用FutureTask并与cancel()或Thread的'throw ThreadDead'异常一起玩,但是我很好奇我是否可以像开始时一样使用它。

thanks! 谢谢!

What I have understand from your question is that you want to cancel the currently running thread if the new thread started. 我从您的问题中了解到的是,如果新线程启动了,您想取消当前正在运行的线程。

This you can do by calling Thread's interrupt() method, this will interrupt the currently running thread, and throws the InterruptedException. 您可以通过调用Thread的interrupt()方法来执行此操作,这将中断当前正在运行的线程,并引发InterruptedException。

Thread t1 = null;
private void exec() {
    t1 = new Thread(new Runnable() {
    public void run() {
    mBind.exec(3);
    }
  }).start();
}

Before calling exec, call t1.interrupt(). 在调用exec之前,请调用t1.interrupt()。

Feels a bit dirty, but could you save the name of the most recently activated Thread and check for it in your Handler ? 感觉有点脏,但是您可以保存最近激活的Thread的名称并在Handler检查它吗? Something like: 就像是:

private static final int MESSAGE_UPDATE_COMPLETE = 0;
private String threadName;

private void exec() {
    Thread thread = new Thread() {
        public void run() {
            // do stuff
            ...
            Message msg = Message.obtain();
            msg.what = MESSAGE_UPDATE_COMPLETE;
            msg.obj = this.getName();
            handler.sendMessage(msg);
        }
    };
    thread.start();
    threadName = thread.getName();
}
...
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
        case MESSAGE_UPDATE_COMPLETE:
            if (threadName.equals((String)msg.obj)) {
                // do UI update
            }
            break;
        ...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM