简体   繁体   English

Android:AsyncTask - 似乎没有异步运行

[英]Android: AsyncTask - doesn't seem to run asynchroneously

I'm trying to use AsyncTask to make a tas run apart from the main thread, thus prevent locking the thread while the task is running. 我正在尝试使用AsyncTask使主线程分开,从而防止在任务运行时锁定线程。 Have I misunderstood the use for AsyncTask? 我误解了AsyncTask的使用吗?

The calling code from the main thread: 来自主线程的调用代码:

public void onClick(View view) {
    int input = Integer.parseInt(editInput.getText().toString());
    String out = "";

    try {
        out = new RunAsyncTask().execute(Integer.toString(input)).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

From the AsyncTask class: 从AsyncTask类:

protected String doInBackground(String... params) {
    long start, stop;
    long result;
    String out = "";
    int input = Integer.parseInt(params[0]);

    // Dalvik (Java) - Recursive:
    start = System.currentTimeMillis();
    result = FibLib.fibJR(input);
    stop = System.currentTimeMillis(); 
    out += String.format("Dalvik (Java) recursive: %d (%d msec)", result, stop - start);

    // Dalvik (Java) - Iterative:
    start = System.currentTimeMillis();
    result = FibLib.fibJI(input);
    stop = System.currentTimeMillis();
    out += String.format("\nDalvik (Java) iterative: %d (%d msec)", result, stop - start);

    return out

From the FibLib class (which is called by the AsyncTask): 从FibLib类(由AsyncTask调用):

public class FibLib {
    public static long fibJR(long n) {  // 
        if (n <= 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }

        return fibJR(n - 1) + fibJR(n - 2);
    }

    // Java implementation - iterative:
    public static long fibJI(long n) { // 
        long previous = -1;
        long result = 1;

        for (long i = 0; i <= n; i++) {
            long sum = result + previous;
            previous = result;
            result = sum;
        }

        return result;
    }   

However, when running this, the main GUI thread is still locked and crashes if the task is more than just a small, quick one. 但是,在运行此操作时,如果任务不仅仅是一个小而快的任务,主GUI线程仍然会被锁定并崩溃。 What am I misunderstanding here? 我在这里误解了什么?

You're calling the AsyncTask 's get() method, which really turns an asynchronous call to synchronous, because it waits for the result of the operation. 你正在调用AsyncTask的get()方法,它实际上将异步调用转为同步,因为它等待操作的结果。 A better way to return the result of the AsyncTask to the calling Activity is to use a callback interface. AsyncTask的结果返回给调用Activity的更好方法是使用回调接口。 Create a simple interface , eg: 创建一个简单的interface ,例如:

interface OnTaskCompletedListener {
    void onTaskCompleted(Object result);
}

Now implement that interface in your Activity class: 现在在Activity类中实现该接口:

public class MainActivity extends Activity implements OnTaskCompletedListener {}

Now you should pass the MainActivity to the AsyncTask object: 现在您应该将MainActivity传递给AsyncTask对象:

AsyncTask task = new RunAsyncTask(onTaskCompletedListener).execute();

And inside the AsyncTask's onPostExecute() you return the result to the listener: AsyncTask's onPostExecute()您将结果返回给侦听器:

onTaskCompletedListener.onTaskCompleted(result);

Now you can use the result in your Activity 's onTaskCompleted() method. 现在,您可以在ActivityonTaskCompleted()方法中使用结果。

Hope the explanation was clear enough and it will help you. 希望解释清楚,它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM