在教程(https://joerglenhard.wordpress.com/2012/05/29/build-windows-service-from-java-application-with-procrun/)后面使用prunsrv.exe在预期的Windows运行下配置了jvm模式的Java服务。但是问题是prunsrv.exe如何停止并启动服务。我正在使用以下类似的start和stop方法将日志打印到具有线程ID的文件中。

 private static boolean stop = false;
    public static void main( String[] args )
    {
    log.debug(Integer.toHexString(System.identityHashCode(Thread.currentThread())));
    if (args.length == 0) {
        log.debug("no args provided, give start/stop as argument");
        return;
    }
    String mode = args[0];
    if ("start".equals(mode)) {
        log.debug("start " + Integer.toHexString(System.identityHashCode(Thread.currentThread())));
        startService(args);
    } else if ("stop".equals(mode)) {
        log.debug("stop " + Integer.toHexString(System.identityHashCode(Thread.currentThread())));
        stopService(args);
    }
    log.debug("End of main " + Integer.toHexString(System.identityHashCode(Thread.currentThread())));
    }


这是以下输出日志(启动和停止服务)

22/Aug/2016 19:22:00,962- App: 441772e
22/Aug/2016 19:22:00,962- App: start 441772e
22/Aug/2016 19:22:00,962- App: startService
22/Aug/2016 19:23:21,259- App: 1ef37254
22/Aug/2016 19:23:21,259- App: stop 1ef37254
22/Aug/2016 19:23:21,259- App: stopService
22/Aug/2016 19:23:21,259- App: End of main 1ef37254
22/Aug/2016 19:23:22,181- App: End of main 441772e


如我们所见,线程是不同的,这意味着启动了用于启动服务和停止服务的新进程。即使变量“ stop”是一个静态布尔值,它们仍然是不同的过程(对吗?)。这是如何运作的?

最佳答案

也许...。我不知道。...prunsrv在新线程(例如“ thread1”)中用选项start调用了主函数,并收听了下一个命令。停止服务时,prunsrv在新线程(例如“ thread2”)中使用选项stop调用了主函数。 prunsrvthread1thread2具有公共内存。

09-25 22:16