如何在AsyncTask中同时执行多个函数?

时间:2017-03-01 04:07:42

标签: java android multithreading android-asynctask google-translate

我有一个asyncTask类,每当用户点击"发送"来自AppCompatActivity的按钮。在我的AsyncTask的doInBackground方法中,我使用Google Translate API将字符串翻译成30种语言。问题是,每个字符串都需要一些时间才能翻译成每种语言。我觉得通过多线程同时运行所有这些函数是一种更好的方法。

每次trycatch都是自己的一组将字符串翻译成1种语言的说明。这种方法太慢了。

这是我的doInBackground功能:

 @Override
protected Void doInBackground(Void... voids) {

    translate = TranslateOptions.newBuilder().setApiKey("GOOGLE_API_KEY").build().getService();

     /*ENGLISH TO ARABIC*/     

    try {
                Translation translation =
                        translate.translate(
                                MY_STRING,
                                Translate.TranslateOption.sourceLanguage("en"),
                                Translate.TranslateOption.targetLanguage("ar"));
                parseObject.put("data0", translation.getTranslatedText());

            } catch (Exception e) {
                parseObject.put("data0", MY_STRING + " " + mContext.getString(R.string.translationfailed));

            }

      /*English to Chinese*/
 try {
                Translation
                        translation =
                        translate.translate(
                                MY_STRING,
                                Translate.TranslateOption.sourceLanguage("en"),
                                Translate.TranslateOption.targetLanguage("zh-CN"));
                parseObject.put("data1", translation.getTranslatedText());

            } catch (Exception e) {
                parseObject.put("data1", MY_STRING + " " + mContext.getString(R.string.translationfailed));

            }

  /*28 More languages run...*/

 /*Then, Save Data into server*/

  parseObject.saveinBackground();

    return null;
}

2 个答案:

答案 0 :(得分:2)

您需要使用多线程。 AsyncTask适用于短时间和短时间轻型操作。 read this tutorial

下载并测试示例代码。

你也可以为每个语言运行一个AsyncTask并传递相同的参数。 但对于多个AsyncTask

class doSomethingInBack extends AsyncTask(String, String, String){}

See this Question

这是线程示例     class PhotoDecodeRunnable实现Runnable {     / *     *定义为此任务运行的代码。     * /     @覆盖     public void run(){     //将当前线程移动到后台     android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

/*
* Stores the current Thread in the PhotoTask instance,
*  that the instance
* can interrupt the Thread.
*/
try {
Translation translation =
translate.translate(
MY_STRING,
Translate.TranslateOption.sourceLanguage("en"),
Translate.TranslateOption.targetLanguage("ar"));
parseObject.put("data0", translation.getTranslatedText());
} catch (Exception e) {
parseObject.put("data0", MY_STRING + " " + mContext.getString(R.string.translationfailed));
}

/*English to Chinese*/
 try {
Translation
translation =
translate.translate(
MY_STRING,
Translate.TranslateOption.sourceLanguage("en"),
Translate.TranslateOption.targetLanguage("zh-CN"));
parseObject.put("data1", translation.getTranslatedText());
} catch (Exception e) {
parseObject.put("data1", MY_STRING + " " + mContext.getString(R.string.translationfailed));
}

}
}

答案 1 :(得分:0)

检查这个答案:

ParserTask parserTask = new ParserTask();
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        parserTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, result);
    }else{
        parserTask.execute(result);
    }