可运行张贴在另一可运行未执行 [英] Runnable posted in another runnable not executed
问题描述
我试图运行从我单身的另一个可运行使用Handler.post()方法可运行的,但第二个可运行没有运行,直到原来的可运行完成后。在这个例子中code下面我从某处应用程序中调用MyManager,的getInstance()。启动命令()。在MyCommand类的myRun变量把线程上,所以我可以测试超时功能睡眠,但可运行称为mTimeoutTimer不后才myRun可运行完成后执行。这究竟是为什么,以及如何改变这种状况?
公共类MyManager {
私人MyManager sInstance;
私人处理器mHandler;
私人的Runnable mTimeoutTimer; 公共静态MyManager的getInstance(){
如果(sInstance == NULL){
sInstance =新MyManager();
}
返回sInstance;
} 私人MyManager(){
mHandler =新的处理程序();
mTimeoutTimer =新的Runnable(){
@覆盖
公共无效的run(){
Log.e(可运行RUNNING!);
}
}; 公共类MyCommand {
私人的Runnable myRun; 公共MyCommand(){
myRun =新的Runnable(){
@覆盖
公共无效的run(){
。MyManager.getInstance()startTimeoutTimer(); 尝试{
视频下载(COMMAND_TIMEOUT_MILLIS * 3);
}赶上(InterruptedException的E){} MyCommand.this.execute();
}
};
} 公共无效的execute(){
myRun.run();
}
}
私人无效startTimeoutTimer(){
mHandler.postDelayed(mTimeoutTimer);
}
公共无效启动命令(){
新MyCommand()执行();
}
}
这是因为处理程序被调用在主线程,因此将等待另外一个到成品
而不是把你的其他处理程序中的 HandlerThread
来在一个单独的线程上运行您的处理程序
HandlerThread线程=新HandlerThread(HandlerThread);
thread.start();
处理程序处理程序=新的处理程序(thread.getLooper());
的文档启动,有一个弯一个新的线程手持类。
然后活套可以用来创建处理程序的类。
需要注意的是启动()仍必须调用。
I'm trying to run a runnable from another runnable using Handler.post() method of my singleton but this second runnable is not being run until after the original runnable finishes. In the example code below I am calling MyManager,getInstance().startCommand() from somewhere in the application. The myRun variable in the MyCommand class has a sleep put on the thread so I can test a timeout feature, but the runnable called mTimeoutTimer is not be executed until AFTER the myRun runnable is finished. Why is this happening, and how can I change that?
public class MyManager{
private MyManager sInstance;
private Handler mHandler;
private Runnable mTimeoutTimer;
public static MyManager getInstance(){
if(sInstance == null){
sInstance = new MyManager();
}
return sInstance;
}
private MyManager(){
mHandler = new Handler();
mTimeoutTimer = new Runnable() {
@Override
public void run() {
Log.e("RUNNABLE RUNNING!");
}
};
public class MyCommand {
private Runnable myRun;
public MyCommand(){
myRun = new Runnable() {
@Override
public void run() {
MyManager.getInstance().startTimeoutTimer();
try {
Thread.sleep(COMMAND_TIMEOUT_MILLIS * 3);
} catch (InterruptedException e) {}
MyCommand.this.execute();
}
};
}
public void execute() {
myRun.run();
}
}
private void startTimeoutTimer(){
mHandler.postDelayed(mTimeoutTimer);
}
public void startCommand(){
new MyCommand().execute();
}
}
That is because Handler are called in the Main thread so it will wait for the other one to finished
instead put your other handler in the HandlerThread
to run your handler on a separate thread
HandlerThread thread = new HandlerThread("HandlerThread");
thread.start();
Handler handler = new Handler(thread.getLooper());
The documentation of HandlerThread
Handy class for starting a new thread that has a looper.
The looper can then be used to create handler classes.
Note that start() must still be called.
这篇关于可运行张贴在另一可运行未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!