java - 多个 Bash 终端处理单个 JVM 实例

标签 java bash jar terminal jvm

我有一个 Java 程序,它接受两个数字作为用户输入并打印总和。

如果 30 秒后没有输入任何内容,程序将终止。

Java代码如下:

public class task {

static double i1;
static double i2;
static boolean breakOut = false;

public static void main(String[] args) throws IOException{

    int x = 30; // time to wait, in seconds

    while(true) {
        boolean goodInput1 = false;
        while(!goodInput1) {
            BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in));
            Date date = new Date();
            SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss");
            System.out.print("Current time: " + ft.format(date));
            System.out.print(" Please enter the first number: ");
            long startTime = System.currentTimeMillis();
                while ((System.currentTimeMillis() - startTime) < x * 1000 && !in1.ready()) {}

            try {
                if (in1.ready()) {
                    i1 = Double.parseDouble(in1.readLine());
                    goodInput1 = true;
                } else {
                    date = new Date();
                    System.out.print("\nTime now is:  " + ft.format(date));
                    System.out.println(" You have not entered any number so exiting the program.");
                    breakOut = true;
                    break;
                }
            } catch (NumberFormatException e) {
                System.out.println("You have entered bad data, please try again.");
                continue;
            }
        }
        if(breakOut) { break; }

        boolean goodInput2 = false;         
        while(!goodInput2) {
            BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
            Date date = new Date();
            SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss");                
            System.out.print("Current time: " + ft.format(date));
            System.out.print(" Please enter the second number: ");
            long startTime2 = System.currentTimeMillis();
                while ((System.currentTimeMillis() - startTime2) < x * 1000 && !in2.ready()) {}
            try {
                if (in2.ready()) {
                    i2 = Double.parseDouble(in2.readLine());
                    goodInput2 = true;
                } else {
                    date = new Date();
                    System.out.print("\nTime now is:  " + ft.format(date));
                    System.out.println(" You have not entered any number, exiting the program.");
                    breakOut = true;
                    break;
                }
            } catch (NumberFormatException e) {
                System.out.println("You have entered bad data, please try again.");
                continue;
            }
        }
        if(breakOut) { break; }

    double sum = i1 + i2;
    System.out.println("The sum is " + sum);

    }

    System.out.println("Program Terminated.");

}

}

我已将程序保存为 Jar 可执行文件。

我编写了一个脚本,用于检查我的 Jar 进程当前是否正在运行,如果没有运行则启动该进程:

My Bash Script

此脚本可确保无论我尝试从多少个不同的 Bash 终端运行 Jar,都只存在一个运行我的程序的 JVM 实例。 (假设我只使用我的脚本来运行该过程)

最终目标是允许连接到单个 JVM 的任何终端都能够通过输入来重置 30 秒计时器。

我想更改脚本来实现此功能。 (更不用说对Java代码的更改了)

我的问题是:

鉴于 JVM 是从一个 Bash 终端启动的,我如何允许另一个终端获取该特定 JVM 实例的句柄?

最佳答案

从操作系统的角度来看,bash终端是一个进程,就像jvm进程一样,你尝试做的是进程间通信。

所以你必须修改java代码以支持进程间功能,例如socket或signal。

终端 bash 进程无法获取另一个进程的能力。

关于java - 多个 Bash 终端处理单个 JVM 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48087748/

相关文章:

java - 如何添加 GlassFish 系统库

java - 签名 Java Applet 中的嵌套类

java - 如何更改 XWPFDocument 中的文本方向?

bash - 为什么这个脚本不改变目录

Linux:在子shell中运行

python - 在 Android 上使用 bash shell 打开浏览器后尝试输入搜索数据

java - Spark-提交找不到类(ClassNotFoundException)

java - java中 boolean 表达式如何工作?

java - 使用连字符时 Spring 表达式语言 (SpEL) 不起作用

带有二维数组的 Java 程序