如何生成多个线程进行上传而无需等待

时间:2015-07-08 10:13:03

标签: android multithreading android-intentservice java-threads

我有以下类负责从收据表中获取未同步的收据并将它们上传到服务器,以下函数现在只是遍历游标结果集:

public class MidnightUpload {


    public static void checkLocalAndUpload(final Context ctx) {

        Cursor cursor = DatabaseHandler
                .getInstance(ctx)
                .getReadableDatabase()
                .query(Receipt.TABLE_NAME, Receipt.FIELDS,
                        Receipt.WEB_RECEIPT_ID + " IS ?", new String[]{"dummy"},
                        null, null,
                        null, null);

        if (cursor != null && cursor.moveToFirst()) {

            do {
                Log.d("_id", cursor.getString(cursor.getColumnIndexOrThrow("_id")));
                Log.d("receipt_id", cursor.getString(cursor.getColumnIndexOrThrow("receipt_id")));
                Log.d("web_receipt_id", cursor.getString(cursor.getColumnIndexOrThrow("web_receipt_id")));
                Log.d("receipt_name", cursor.getString(cursor.getColumnIndexOrThrow("receipt_name")));
              //  Log.d("image", cursor.getString(cursor.getColumnIndexOrThrow("image")));
                Log.d("date_added", cursor.getString(cursor.getColumnIndexOrThrow("date_added")));
                Log.d("status", cursor.getString(cursor.getColumnIndexOrThrow("status")));
                Log.d("currency", cursor.getString(cursor.getColumnIndexOrThrow("currency")));
                Log.d("category", cursor.getString(cursor.getColumnIndexOrThrow("category")));
                Log.d("sub_category", cursor.getString(cursor.getColumnIndexOrThrow("sub_category")));
                Log.d("payment", cursor.getString(cursor.getColumnIndexOrThrow("payment")));
                Log.d("invoice", cursor.getString(cursor.getColumnIndexOrThrow("invoice")));
                Log.d("custom_field", cursor.getString(cursor.getColumnIndexOrThrow("custom_field")));
                Log.d("organization", cursor.getString(cursor.getColumnIndexOrThrow("organization")));
                Log.d("person", cursor.getString(cursor.getColumnIndexOrThrow("person")));

            } while (cursor.moveToNext());
        }


    }
}

我知道我可以使用以下方式启动多个异步任务:

 asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);

我打算从IntentService调用上面的方法。所以这是我的困惑:

1)do while循环是否会等到控制从asyncTask返回到下一次迭代?

2)在intentService中使用和生成多个线程会破坏我的程序吗?

3)我最好使用Runnable r = new Runnable()而不是AsyncTask - 因为我不想进行任何UI操作?

0 个答案:

没有答案