我想通过远程桌面连接上的完整计算机名称连接计算机.在nodejs中,我创建了一个子进程来执行cmd命令.它成功执行,但两分钟后再次执行.我使用了child_process模块的kill方法,它不起作用.
var child_process = require('child_process');
child_process.exec('mstsc /v ' + fullName, function(err, stdout, stderr) {
if(err){
console.log(err);
}
});
child_process.kill();
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?非常感谢你!
在《Effective Java》一书中:
// Broken! - How long would you expect this program to run?
public class StopThread {
private static boolean stopRequested;
public static void main(String[] args) throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
public void run() {
int i = 0;
while (!stopRequested)
i++;
}
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
}
Run Code Online (Sandbox Code Playgroud)
背景线程不会在一秒后停止。因为JVM、HotSpot服务器VM中的提升、优化都是如此。
您可以在以下主题中查看这一点:
为什么 HotSpot 将使用提升来优化以下内容?。
优化过程是这样的:
if (!done)
while (true)
i++;
Run Code Online (Sandbox Code Playgroud)
有两种方法可以解决该问题。
private static volatile …Run Code Online (Sandbox Code Playgroud)