关闭钩子可以依赖另一个线程吗?

发布于 2024-08-08 19:13:02 字数 217 浏览 3 评论 0原文

在关闭挂钩方法中,我需要它向另一个进程发送消息以表明它已关闭。我当前的消息处理代码要求将消息写入队列,该队列由另一个线程处理并发送到它要去的任何地方。在这种情况下,由另一个线程写入管道文件。

在关闭钩子中,我可以保证这些线程仍然在运行吗?我有几个关闭挂钩,但这些都是为了处理不需要其他线程的其他事情。

执行是最小的。它将运行大约 15 行代码以及写入文件所需的任何等待,这也应该是最少的。

In a shutdown hook method, I need it to send a message to another process to say it has shutdown. My current message handling code requires that the message be written into a queue, this queue is processed by another thread and sent on to wherever it is going. In this case, to be written into a pipe file by another thread.

In a shutdown hook, can I guarantee that these threads will still be running? I have several shutdown hooks but these are all to handle other things that don't need other threads.

The execution is minimal. It will run about 15 lines of code + any wait needed to write into the file which should also be minimal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-08-15 19:13:02

来自 addShutdownHook

“请注意,守护线程将在关闭序列期间继续运行,如果通过调用 exit 方法启动关闭,则非守护线程也会继续运行。”< /em>

鉴于此,我想说可以安全地假设您的线程仍将运行并且您能够与其通信。我唯一要警告的是不要依赖以确定性顺序运行的关闭挂钩。

From the Javadoc description of addShutdownHook:

"Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method."

Given this I would say that it is safe to assume that your thread will still be running and that you're able to communicate with it. Only thing I would warn against is relying on your shut-down hooks running in a deterministic order.

筱武穆 2024-08-15 19:13:02

听起来您需要一个多步骤的关闭过程。在多线程环境中,我不会指望简单地评估您需要执行的代码行,以便为您提供有关关闭过程的指导......

It sounds like you need a multi-step shutdown process. In a multithreaded environment, I wouldn't count on simply evaluating the lines of codes you need to execute in order to give you guidance on your shutdown procedure...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文