Android 方法 onCreate() 中的线程在 10% 的情况下仍会停止 UiThread
这是我的应用程序的第一个 Activity 的 onCreate 方法。它初始化所有 Widget,然后启动一个线程从互联网加载模型数据。在某些情况下,当网络非常慢或没有响应时,“initializeModelThread”似乎会占用整个 CPU 并且不允许 onCreate 方法完成。
在一种情况下,我看到 Log.v("Time...)
在 300 毫秒后被调用,但屏幕保持黑色大约 20 秒,直到 HttpException 停止 initalizeModelThread
>. 您已经可以看到我将线程的优先级设置为低。
在这种情况下,使用 AsyncTask 有帮助吗?
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
Long time = System.currentTimeMillis();
initializeWidgets();
Thread threadInstance = new Thread(initializeModelThread);
threadInstance.setPriority(Thread.MIN_PRIORITY);
threadInstance.start();
Log.v("Time", "Difference from start " + Long.toString(System.currentTimeMillis() - time));
}
This is my onCreate method of my first activity of my app. It initalizes all Widgets and then starts a Thread to load model data from the internet. In some cases, when the network is really slow or not responding, the "initializeModelThread"
seems to take the whole CPU and doesnt allow the onCreate Methode to finish.
In one case I saw that Log.v("Time...)
was called after 300 ms, but the screen remained black for about 20 seconds until a HttpException stopped the initalizeModelThread
. You already can see that I set the Priority of the Thread to low.
Would the use of AsyncTask help in this case? Do u have any other thoughts about that issue?
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
Long time = System.currentTimeMillis();
initializeWidgets();
Thread threadInstance = new Thread(initializeModelThread);
threadInstance.setPriority(Thread.MIN_PRIORITY);
threadInstance.start();
Log.v("Time", "Difference from start " + Long.toString(System.currentTimeMillis() - time));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AsyncTask
在这里不会产生任何影响。不要在
onCreate()
方法上创建线程,而是在onStart()
方法上创建线程。AsyncTask
would not make a difference here.Instead of creating the thread on the
onCreate()
method, do it on theonStart()
method.问题是我在“initializeWidgets”中使用了谷歌地理反向编码功能(GeoCoder),它从网络下载数据。在 90% 的情况下,速度确实很快。现在,将下载移至 AsyncTask 后启动时再也不会出现这样的延迟
the problem was that I used the google geo reverse coding feature (GeoCoder) in "initializeWidgets", which downloaded data from the web. It was really fast in 90 % of the cases. Now there never was such a delay again while starting after moving that download into a AsyncTask