英文:
Releasing CountDownLatch only when I receive a callback in one of my Thread
问题
在这种情况下,何时调用doneSignal.await()
?
我的要求是等待回调接收到之前等待返回调用。
我尝试在其他线程和主线程中调用它,但没有运气,回调永远不会被接收,从而冻结应用程序,最初当我不使用CountDownLatch时,getCall立即返回null值,而不等待回调。以下是我的代码:
public UserSubscriptions getDocument(final String uid) {
final UserSubscriptions[] userSubscriptions = new UserSubscriptions[1];
final CountDownLatch doneSignal = new CountDownLatch(1);
try {
Thread getCall = new Thread(new Runnable() {
@Override
public void run() {
try {
DocumentReference docRef = db.collection("xyz").document(uid);
docRef.get()
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
userSubscriptions[0] = null;
TimerLog.e("document retrieve failed");
doneSignal.countDown();
}
})
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
userSubscriptions[0] = documentSnapshot.toObject(UserSubscriptions.class);
TimerLog.e("document retrieved successfully " + userSubscriptions[0].toString());
doneSignal.countDown();
}
});
TimerLog.e("control exited from run");
} finally {
TimerLog.e("Nads in finally");
}
}
});
getCall.start();
TimerLog.e("before");
doneSignal.await();
TimerLog.e("after");
} catch (InterruptedException e) {
TimerLog.e("exception in await " + e.getLocalizedMessage());
}
return userSubscriptions[0];
}
控制台输出:
before
control exited from run
in finally
请注意,doneSignal.await()
是在getCall
线程之后调用的,它会等待直到doneSignal.countDown()
被调用。这样做的目的是确保在回调成功或失败时都会唤醒等待的线程。
英文:
When to invoke doneSignal.await()
in this scenario??
My requirement is to wait the return call until callback is received.
I tried invoking it in other thread and also in main thread. But no luck, callback is never received thereby freezing the app and initially when I was not using the CountDownLatch getCall was returning null value immediately without waiting for the callback. Below is my code
public UserSubscriptions getDocument(final String uid) {
final UserSubscriptions[] userSubscriptions = new UserSubscriptions[1];
final CountDownLatch doneSignal = new CountDownLatch(1);
try {
Thread getCall = new Thread(new Runnable() {
@Override
public void run() {
try{
DocumentReference docRef = db.collection("xyz").document(uid);
docRef.get()
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
userSubscriptions[0] = null;
TimerLog.e("document retrieve failed");
doneSignal.countDown();
}
})
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
userSubscriptions[0] = documentSnapshot.toObject(UserSubscriptions.class);
TimerLog.e("document retrieved successfully " + userSubscriptions[0].toString());
doneSignal.countDown();
}
});
TimerLog.e("control exitted from run");
} finally {
TimerLog.e("Nads in finally");
}
}
});
getCall.start();
TimerLog.e("before");
doneSignal.await();
TimerLog.e("after");
} catch (InterruptedException e) {
TimerLog.e("exception in await "+e.getLocalizedMessage());
}
return userSubscriptions[0];
}
Console Output :
before
control exitted from run
in finally
答案1
得分: 0
这个CountDownLatch对我没有用,也没有任何互斥锁的东西。做这种类型的事情的最佳方式是,当你从服务器接收到回调时,不应该让那个函数返回任何东西。简单的方法是使用回调功能。
英文:
This CountDownLatch did not work for me neither any mutual exclusive lock thing. The best way to do such type of thing where you are receiving a callback from server then you should not have that function return anything. Simple method is to have callback functionality.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论