只有在我的一个线程中收到回调时才释放 CountDownLatch。

huangapple go评论109阅读模式
英文:

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(&quot;xyz&quot;).document(uid);
docRef.get()
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
userSubscriptions[0] = null;
TimerLog.e(&quot;document retrieve failed&quot;);
doneSignal.countDown();
}
})
.addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
userSubscriptions[0] = documentSnapshot.toObject(UserSubscriptions.class);
TimerLog.e(&quot;document retrieved successfully &quot; + userSubscriptions[0].toString());
doneSignal.countDown();
}
});
TimerLog.e(&quot;control exitted from run&quot;);
} finally {
TimerLog.e(&quot;Nads in finally&quot;);
}
}
});
getCall.start();
TimerLog.e(&quot;before&quot;);
doneSignal.await();
TimerLog.e(&quot;after&quot;);
} catch (InterruptedException e) {
TimerLog.e(&quot;exception in await &quot;+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.

huangapple
  • 本文由 发表于 2020年7月31日 15:12:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63187330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定