I've just started playing around with CountDownLatch
in my Android app. Currently I am trying to make two Volley
requests to my api, and wait until the data has been retrieved and stored before continuing with thread execution.
This is a sample of my code:
// new CountDownLatch for 2 requests
final CountDownLatch allDoneSignal = new CountDownLatch(2);
transactions.getResourcesForRealm(Contact.class, "", new ICallBack<Contact>() {
@Override
public void onSuccess(ArrayList<Contact> resources, String resourceId) {
transactions.createRealmObject(resources, Contact.class);
allDoneSignal.countDown();
}
@Override
public void onFail(ArrayList<Contact> resources) {
}
});
transactions.getResourcesForRealm(Meeting.class, "", new ICallBack<Meeting>() {
@Override
public void onSuccess(ArrayList<Meeting> resources, String resourceId) {
transactions.createRealmObject(resources, Meeting.class);
allDoneSignal.countDown();
}
@Override
public void onFail(ArrayList<Meeting> resources) {
}
});
try {
allDoneSignal.await();
// continue executing code
// ...
} catch (InterruptedException e) {
e.printStackTrace();
}
The issue is that it doesn't seem to "complete" the countdown and therefore freezes because the latch is never released. I have confirmed that the API requests are working and the onSuccess
callback is hit successfully, but the thread hangs.
UPDATE
I've just noticed that with the CountDownLatch
set to 0, it hits onSuccess
, but when I set it to anything greater than 0, it freezes and onSuccess
is never called. Seems like something's funky with the threading.
Volatile variable
concept. – SoughLog.wtf
this... – VoltaismallDoneSignal
variable is the same in the three places (if the code is exactly like you posted in a single method then it obviously is)... – Sahaptin