gpt4 book ai didi

java - 如何从 Firebase Java Admin SDK 停止线程

转载 作者:行者123 更新时间:2023-11-30 06:30:47 25 4
gpt4 key购买 nike

通过运行示例代码 https://github.com/firebase/firebase-admin-java/blob/master/src/test/java/com/google/firebase/database/integration/ShutdownExample.java

public class ShutdownExample {

public static void main(String[] args) {
final Semaphore shutdownLatch = new Semaphore(0);

FirebaseApp app =
FirebaseApp.initializeApp(
new FirebaseOptions.Builder()
.setDatabaseUrl("https://admin-java-sdk.firebaseio.com")
.build());

FirebaseDatabase db = FirebaseDatabase.getInstance(app);
db.setLogLevel(Level.DEBUG);
DatabaseReference ref = db.getReference();

ValueEventListener listener =
ref.child("shutdown")
.addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Boolean shouldShutdown = snapshot.getValue(Boolean.class);
if (shouldShutdown != null && shouldShutdown) {
System.out.println("Should shut down");
shutdownLatch.release(1);
} else {
System.out.println("Not shutting down: " + shouldShutdown);
}
}

@Override
public void onCancelled(DatabaseError error) {
System.err.println("Shouldn't happen");
}
});

try {
// Keeps us running until we receive the notification to shut down
shutdownLatch.acquire(1);
ref.child("shutdown").removeEventListener(listener);
db.goOffline();
System.out.println("Done, should exit");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

调用db.goOffline()后,仍有一些线程在运行:

Reference Handler Finalizer Signal Dispatcher FirebaseDatabaseWorker pool-5-thread-1

如何清洁它?

最佳答案

离线仅意味着 Firebase 实时数据库 SDK 将不再进行联网。这并不意味着它的线程必须消失或者程序应该退出。如果您完成工作并想要退出该进程,请调用 System.exit(0)

关于java - 如何从 Firebase Java Admin SDK 停止线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com