据我所知,我实现了一个在新线程上创建的可运行对象。但是,该线程似乎未在后台运行,并且在可运行内部执行的操作使UI停止执行繁重的操作。
见下文:
custListLoadThread = new Thread(loadRunnable);
custListLoadThread.run();
private Runnable loadRunnable = new Runnable()
{
@Override
public void run()
{
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
Gen.popup("TEST"); // Creates a toast pop-up.
// This is to know if this runnable is running on UI thread or not!
try
{
customers = Db.BasicArrays.getCustomers(CustomApp.Session.businessCode, empId);
runOnUiThread(new Runnable() {
@Override
public void run() {
populate();
setCustListVisible(true);
loading = false;
}
});
}
catch (final Exception ex)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Gen.popup(ex.getMessage());
}
});
}
}
};
但是,此代码未在后台运行,它似乎仍在UI线程上运行。我已经放置了这一行Gen.popup("TEST");
来确保这一点(toast
在非UI线程中调用弹出窗口会导致错误)。
关于为何此可运行对象不在后台运行的任何想法?
custListLoadThread = new Thread(loadRunnable);
custListLoadThread.start();
您需要启动线程,而不是在当前线程中调用run()方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句