如何在Android中暂停Thread的消息队列?

我通过Handler.post()将一堆runnable排队到一个线程中。 我希望能够向该线程发送一条应该暂停的注释。

暂停我的意思是,完成您当前正在处理的runnable或消息,但是在我告诉您继续之前,不要转到下一条消息或在消息队列中运行。

如果其他人找到了解决这个问题的方法,我最终会使用ThreadPoolExecutor,使用其文档中的示例代码创建一个PausableThreadPoolExecutor: http : //download.oracle.com/javase/1.5.0/docs/api /java/util/concurrent/ThreadPoolExecutor.html

它不使用Message Queue或Looper,但它完成了我尝试做的事情,即创建一个可以暂停和/或取消的Runnables队列。 它还具有能够在指定数量的线程上传播队列的额外好处。

在Java中没有用Thread实现Thread的暂停方法,但你可以使用boolean模拟一个暂停,等待最后一条消息完成是你可以在boolean的setter方法中做的事情,

public void run(){ while(!paused && !finised){ // work } } public void setPause(boolean paused){ //wait for your last message if there is one and then this.paused = paused; } 

并且你应该使用另一个布尔值来知道线程是否已经完成,以退出while。