简体   繁体   English

Android:等待几个异步任务完成

[英]Android: wait for several Async Tasks to complete

I have to make several calls to save data into my database. 我必须打几个电话才能将数据保存到数据库中。 I have to wait for all the calls to finish and then move further. 我必须等待所有通话结束,然后再继续。 I am using CountDownLatch and ExecutorService to achieve this. 我正在使用CountDownLatch和ExecutorService来实现这一点。 Following is my code: 以下是我的代码:

    private void saveData(double latitude, double longitude) {
    try {
        performParallelTask(getListOfLatLngPair(latitude, longitude));
        preformPostTask();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void performParallelTask(List<LatLngPair> latLngPairList) throws InterruptedException {
    CountDownLatch doneLatch = new CountDownLatch(latLngPairList.size());
    ExecutorService executorService = Executors.newFixedThreadPool(latLngPairList.size());
    for (int i = 0; i < latLngPairList.size(); i++) {
        executorService.execute(new StopLatchedThread(doneLatch, latLngPairList.get(i)));
    }
    doneLatch.await();
}

public class StopLatchedThread implements Runnable {
    private final CountDownLatch stopLatch;
    private final LatLngPair latLngpair;

    public StopLatchedThread(CountDownLatch stopLatch, Fence latLngpair
                            ) {
        this.stopLatch = stopLatch;
        this.latLngpair = latLngpair;
    }

    public void run() {
        try {
            addLatLngPair();
        } finally {
            stopLatch.countDown();
        }
    }

    private void addLatLngPair() {
        Log.i(LOG_AREA, "add fence method");
        Engine.AddLatLngPair(latLngPair, new Engine
            .LatLngPairOperationListener() {
            @Override
            public void Succeed() {
                // do noting
            }

            @Override
            public void Failed(int reason) {
                Log.i(LOG_AREA, "Failed to add pair"
            }
        });
    }
}

private void preformPostTask() {
    Intent intent = new Intent(PairListActivity.this, PairListMapActivity.class);
    atartActivity(intent);
}

When I try to execute code, It starts Activity before threads finish their execution. 当我尝试执行代码时,它将在线程完成执行之前启动Activity。 May I know what is wrong in my code. 我可以知道我的代码有什么问题吗?

I strongly believe that Engine.AddLatLngPair is a asynchronous call therefore you have to call stopLatch.countDown(); 我坚信Engine.AddLatLngPair是异步调用,因此您必须调用stopLatch.countDown(); in _Succeed()_ and _ Failed(int reason)_ methods _Succeed()_和_ Failed(int reason)_方法中

By the way - you don't threads and executor service over there, you can just use 顺便说一句-您不必在那儿执行线程和执行程序服务,而只需使用

public void performParallelTask(List<LatLngPair> latLngPairList) throws InterruptedException {
CountDownLatch doneLatch = new CountDownLatch(latLngPairList.size());
for (int i = 0; i < latLngPairList.size(); i++) {
    addLatLngPair(doneLatch, latLngPairList.get(i));
}
doneLatch.await();

} }

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

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