是一个很好的做法创建匿名AsyncTask的并行少量已知冻结过程? [英] Is a good practice create anonymous AsyncTask for parallel small known freeze process?
本文介绍了是一个很好的做法创建匿名AsyncTask的并行少量已知冻结过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:你打算做一些事情,这将需要几秒钟,不想冻结您的UI thred,对不对?你可以使用一个AsyncTask的,但你不想创建一个外部(或内部)类来解决一个小问题,冻结
E.g.: you gonna do something that will take a few seconds and don't wanna freeze your UI thred, right? You could use an AsyncTask but you don't wanna create a external (or inner) class to solve a small freeze problem.
那么,是一个很好的初步实践办呢?
So, is a good pratice do it?
package com.example.stackoverflowsandbox;
import android.os.AsyncTask;
public class Foo {
// E.g. before call foo method you change you Activity to loading state.
private void foo() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground( final Void ... params ) {
// something you know that will take a few seconds
return null;
}
@Override
protected void onPostExecute( final Void result ) {
// continue what you are doing...
Foo.this.continueSomething();
}
}.execute();
}
private void continueSomething() {
// some code...
}
}
我面对,当我COM pressing位图和循环大阵更新项目里面的一些数据。
I've faced with that when I compressing Bitmaps and looping big array to update some data inside items.
推荐答案
是的,但不是你做的方式。
Yes, but not the way you do it.
记住,开始蜂窝 AsyncTasks的默认执行模式是串行
new AsyncTask<Void, Void, Void>() {
....
....
}.execute(); <------ serial execution
而是使用一个线程池执行人:
Instead use a thread pool executor:
new AsyncTask<Void, Void, Void>() {
....
....
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null); <------ parallel execution
这篇关于是一个很好的做法创建匿名AsyncTask的并行少量已知冻结过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文