您可以分配唯一的线程 ID 并从外部程序访问线程吗?
我目前正在实现一个需要我处理线程和进程的程序。
IDEA:
有多个java进程在运行,每个进程可能有多个线程。 当前的java实现是这样的,java中的线程id对于特定进程来说是唯一的,但在进程内不是唯一的。那么有没有一种方法可以在多个进程之间实现唯一的线程ID?
此外,我需要实现一个外部 java 程序来监视这些线程。我所说的监视是指,根据某些逻辑,我需要通知特定线程(使用唯一的线程 ID)有关事件的信息。有没有办法可以从外部程序访问线程。如果是,怎么办?
还有其他解决方案可以实现类似的想法吗?
先感谢您。
I am currently implementing a program that requires me to handle threads and process.
IDEA:
There are multiple java processes running and each process may have multiple threads.
Current java implementation is such that thread ids in java is unique for a particular process but not within the processes. So is there a way I could implement a unique thread ids among multiple processes?Also, I need to implement an external java program that monitors these threads. By monitoring I mean, depending upon some logic I need to notify a particular thread(using unique thread id) regarding an event. Is there a way that I can access thread from external program. If yes how?
Are there any other solutions to implement the similar idea?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用进程 id 和线程 id 的串联来唯一标识一个线程 - 例如,进程
7038
中的线程23
可以标识为7038: 23
。这样做的好处是,给定线程标识符,您可以知道该线程属于哪个进程。我怀疑一个进程是否有可能控制另一个进程的线程。您可能需要使用某种形式的进程间通信,例如 RMI、命名管道或 TCP。每个进程可能应该有一个线程,用于等待传入消息、解析消息,并根据消息的内容通知相应的线程。
基于 TCP 的解决方案的一个非常简单示例如下:每个工作进程都有一个线程,用于侦听来自监视进程的 TCP 连接;预计当监控进程连接时,它会写入一行包含该工作进程中线程的id。工作进程必须保留例如将线程ID映射到
Thread
对象的HashMap
。也许还应该有一种方法让监控进程向工作进程询问其所有线程 ID。工作进程可以简单地维护一个进程 ID 列表(以及每个进程侦听哪个端口),以及对于每个进程 ID,维护该进程内的线程 ID 列表。
顺便说一句,正如 @parsifal 所说,了解您实际想要实现的目标会很有趣。
You could use a concatenation of the process id and the thread id to uniquely identify a thread - for instance, thread
23
in process7038
could be identified as7038:23
. This has the advantage that given a thread identifier, you can tell which process the thread belongs to.I doubt that it is possible for one process to control the threads of another. You probably need to use some form of inter-process communication, such as RMI, named pipes, or TCP. Each process should probably have one thread that waits for an incoming message, parses it, and notifies the appropriate thread based on the contents of the message.
A very simple example of what a TCP-based solution might look like: Every worker process has a thread that listens for TCP connections from the monitoring process; it is expected that when the monitoring process connects, it will write one line containing the id of a thread in this worker process. The worker process must keep e.g. a
HashMap
that maps thread ids toThread
objects.There should probably also be a way for the monitoring process to ask a worker process for all its thread ids. The worker process can simply maintain a list of process ids (and which port each process listens to) and, for each process id, a list of the thread ids inside that process.
By the way, as @parsifal said, it would be interesting to know what you are actually trying to achieve.