我可以在同一个活动中同时运行多个装载程序吗?

时间:2017-08-26 09:00:31

标签: android android-asynctask loader asynctaskloader

我对using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace testLock { class Program { public static void Main() { // Start a thread that calls a parameterized static method. for(int i = 0; i< 10;i++) { Thread newThread = new Thread(DoWork); newThread.Start(i); } Console.ReadLine(); } static object gObject= new object(); public static void DoWork(object data) { int len = (int)data % 3; object tmp = new object(); Console.WriteLine("to lock...... Data='{0}' sleepTime:{1}", data, len); lock (tmp)//tmp won't work, change tmp to gObject to see different output, which is good locking case) { Console.WriteLine("in lock...... Data='{0}' sleepTime:{1}", data, len); Thread.Sleep( len* 1000); Console.WriteLine("Static thread procedure. Data='{0}' sleepTime:{1}", data, len); } } } } **Lock temp variable,will output:** to lock...... Data='1' sleepTime:1 in lock...... Data='1' sleepTime:1 to lock...... Data='2' sleepTime:2 in lock...... Data='2' sleepTime:2 to lock...... Data='0' sleepTime:0 in lock...... Data='0' sleepTime:0 Static thread procedure. Data='0' sleepTime:0 to lock...... Data='3' sleepTime:0 in lock...... Data='3' sleepTime:0 Static thread procedure. Data='3' sleepTime:0 to lock...... Data='4' sleepTime:1 in lock...... Data='4' sleepTime:1 to lock...... Data='5' sleepTime:2 in lock...... Data='5' sleepTime:2 to lock...... Data='6' sleepTime:0 in lock...... Data='6' sleepTime:0 Static thread procedure. Data='6' sleepTime:0 to lock...... Data='7' sleepTime:1 in lock...... Data='7' sleepTime:1 to lock...... Data='8' sleepTime:2 in lock...... Data='8' sleepTime:2 to lock...... Data='9' sleepTime:0 in lock...... Data='9' sleepTime:0 Static thread procedure. Data='9' sleepTime:0 Static thread procedure. Data='1' sleepTime:1 Static thread procedure. Data='4' sleepTime:1 Static thread procedure. Data='7' sleepTime:1 Static thread procedure. Data='2' sleepTime:2 Static thread procedure. Data='5' sleepTime:2 Static thread procedure. Data='8' sleepTime:2 **Then lock gObject, will print:** to lock...... Data='0' sleepTime:0 in lock...... Data='0' sleepTime:0 to lock...... Data='1' sleepTime:1 to lock...... Data='2' sleepTime:2 Static thread procedure. Data='0' sleepTime:0 in lock...... Data='1' sleepTime:1 to lock...... Data='3' sleepTime:0 to lock...... Data='4' sleepTime:1 to lock...... Data='5' sleepTime:2 to lock...... Data='6' sleepTime:0 to lock...... Data='7' sleepTime:1 to lock...... Data='8' sleepTime:2 to lock...... Data='9' sleepTime:0 Static thread procedure. Data='1' sleepTime:1 in lock...... Data='5' sleepTime:2 Static thread procedure. Data='5' sleepTime:2 in lock...... Data='9' sleepTime:0 Static thread procedure. Data='9' sleepTime:0 in lock...... Data='2' sleepTime:2 Static thread procedure. Data='2' sleepTime:2 in lock...... Data='8' sleepTime:2 Static thread procedure. Data='8' sleepTime:2 in lock...... Data='7' sleepTime:1 Static thread procedure. Data='7' sleepTime:1 in lock...... Data='4' sleepTime:1 Static thread procedure. Data='4' sleepTime:1 in lock...... Data='3' sleepTime:0 Static thread procedure. Data='3' sleepTime:0 in lock...... Data='6' sleepTime:0 Static thread procedure. Data='6' sleepTime:0 的使用存有疑问。 就我而言,我调用自己的类Loader并返回AsyncTaskLoader的MyObject。

List

我的问题是:我可以在同一public class MyActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<MyObject>> { @Override protected void onCreate(Bundle savedInstanceState) { ... getLoaderManager().initLoader(1, null, this); getLoaderManager().initLoader(2_ID, null, this); getLoaderManager().initLoader(3_ID, null, this); ... } } 上同时运行多个Loader吗?

每个Loader在Activity方法完成后,是否会调用我loadInBackground()中的onLoadFinished()方法?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

是的,我们可以在一个具有唯一ID的活动中一次运行多个加载器,因为Loader是一个异步过程。请在下面找到示例模板。

public class SampleActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample);
        initLoaders();
    }

    private void initLoaders() {
        getLoaderManager().initLoader(1, null, null);
        getLoaderManager().initLoader(2, null, null);
        getLoaderManager().initLoader(3, null, null);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader = null;
        switch (id) {
            case 1:
                cursorLoader = new CursorLoader(this, CONTENT_URI,
                        PROJECTION, selection, null, null);
                break;

            case 2:
                cursorLoader = new CursorLoader(this, CONTENT_URI,
                        PROJECTION, selection, null, null);
                break;

            case 3:
                cursorLoader = new CursorLoader(this, CONTENT_URI,
                        PROJECTION, selection, null, null);
                break;
        }
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        switch (loader.getId()) {
            case 1:
                if (data != null && data.getCount() > 0)
                    setData1();
                break;

            case 2:
                if (data != null && data.getCount() > 0)
                    setData2();
                break;

            case 3:
                if (data != null && data.getCount() > 0)
                    setData3();
                break;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}