java Firebase:延迟退出直到写入完成 [英] java Firebase: delay exit until writes finish
问题描述
我有一个用Java编写的服务器,它可以在监听,处理和更新Firebase数据时接收关闭信号。由于Firebase线程是Java中的守护线程,我想为主线程添加一些延迟,以允许写操作完成。
I have a server written in Java that can receive a shut down signal while it's listening to, processing, and updating Firebase data. Since the Firebase threads are daemon threads in Java, I wanted to add some delay to the main thread to allow write operations to complete.
我当前的想法是锁存方法:使用一些并发计数器来跟踪挂起的写操作,并让主线程退出时没有。计数器将在 onComplete()
回调中更新,所以我想知道:
My current idea is the latch approach: use some concurrent counter to track pending writing operations, and let the main thread exit when there are none. The counter would get updated in onComplete()
callbacks, so I was wondering:
在Firebase客户端,可能 onComplete()
回调永远不会被调用?是否有合理的死锁危险?
In the Firebase client, when might onComplete()
callbacks never get called? Is there reasonable danger of deadlock?
推荐答案
取自这个google群组讨论。这使用布尔 done
(在这种情况下为线程安全操作的AtomicBoolean)和 while
循环。 >
Taken from this google group discussion. This uses a boolean done
(an AtomicBoolean for thread safe ops in this case) and a while
loop.
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;
public class Demo {
public static void main(String[] args) {
final AtomicBoolean done = new AtomicBoolean(false);
Firebase ref = new Firebase("https://testjava.firebaseio-demo.com/");
ref.setValue(new Date().toString(), new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
done.set(true);
}
});
while (!done.get());
}
}
这篇关于java Firebase:延迟退出直到写入完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!