简体   繁体   English

如何在同一系统中的两个Java进程之间共享变量/块?

[英]How can i share the variable/block between two java process in same system?

Let's assume I have a Java program with a main class in persondetails.java . 假设我在persondetails.java有一个带有主类的Java程序。

Person1 runs persondetails.java to get their details. Person1运行persondetails.java以获取其详细信息。 When a second person tries to access the same program in same system, it should say someone is already using this program, wait until the specific block completes in person1. 当第二个人尝试访问同一系统中的同一程序时,应该说有人已经在使用该程序,请等到person1中的特定块完成。

How can I communicate between these two Java processes in the same system? 如何在同一系统中的这两个Java进程之间进行通信?

If I understand your question correctly you would like to have a program that only allows one instance to be running at a time? 如果我正确理解了您的问题,您是否想要一个程序,一次只允许一个实例运行? I can think of two possibilities off the top of my head. 我可以想到两种可能性。

  • Using some kind of "lock" file on the filesystem. 在文件系统上使用某种“锁定”文件。 When the program is executed it could check for the existence of the lock file and display an error if it already exists. 执行该程序时,它可以检查锁定文件的存在并显示错误(如果已存在)。 If the lock file doesn't exist it would be created and the program allowed to continue. 如果锁定文件不存在,则会创建该文件,并允许该程序继续运行。 You would just have to make sure you delete the lock file when the program finishes. 您只需要确保在程序完成时删除锁定文件即可。

  • Binding to a specific port (that is unlikely to already being used). 绑定到特定端口(不太可能已经被使用)。 When the program is executed you would attempt to bind to the port, if you can the program continues otherwise you give an error. 执行程序时,您将尝试绑定到端口,如果可以,程序将继续执行,否则会出错。

Either way you are just using a locking mechanism to detect that the other process is running. 无论哪种方式,您都只是使用锁定机制来检测其他进程是否正在运行。

Locking using the file system can be problematic. 使用文件系统锁定可能会出现问题。 For example if the program fails and stops executing unexpectedly the lock file could remain and prevent future execution of the program. 例如,如果程序失败并意外停止执行,则锁定文件可能会保留并阻止程序将来执行。 At the same time binding to a port to accomplish this is really a misuse of that functionality and could be problematic in it's own way (port already in use, or the application not being allowed to bind to the port). 同时绑定到端口以完成此操作实际上是对该功能的滥用,并且可能以其自己的方式出现问题(端口已在使用中,或者不允许应用程序绑定到端口)。

I personally would be carefully considering whether or not it is really necessary to do this kind of locking between different processes. 我个人会仔细考虑是否真的有必要在不同进程之间进行这种锁定。 I would consider it favorable to avoid using either of the options I mentioned previously. 我认为最好避免使用前面提到的任何一种方法。

At a very very basic level, the program could create a file named lock. 在非常基本的级别上,该程序可以创建一个名为lock的文件。 The process would be something like 该过程将类似于

  1. Process A checks for existence of lock. 进程A检查锁是否存在。
  2. If lock does not exist, Process A creates lock and begins to work. 如果不存在锁,则进程A创建锁并开始工作。
    1. At the end of work, Process A deletes lock. 在工作结束时,进程A删除锁。
  3. If lock exists, Process A waits until lock does not exist. 如果存在锁,则进程A等待直到锁不存在。
    1. Process A can poll or wait for an interrupt or any of a variety of callback mechanisms. 进程A可以轮询或等待中断或各种回调机制中的任何一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM