简体   繁体   English

在运行时停止线程

[英]Stop thread with while running

I'm making a keylog and the code of keylog is in a class extends Thread. 我正在制作一个键盘日志,键盘日志的代码在Thread扩展类中。 And I put a boolean running like variable and I have this code : 我把一个布尔值像变量一样运行,我有下面的代码:

public void run() {
    while(running)
        try {
            Config conf = new Config();
            System.setOut(new PrintStream(new File("keylog/output-file.txt")));

            GlobalScreen.registerNativeHook();

            GlobalScreen.getInstance().addNativeKeyListener(new Keylog(conf));

        } catch (FileNotFoundException | NativeHookException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

And i have another void method to stop thread 我还有另一种无效的方法来停止线程

void stopKeylog() {
    running=false;
}

But when, I stop thread, I will see the output txt file and is with bugs. 但是,当我停止线程时,我将看到输出txt文件并且存在错误。 And when I remove the while(running) the output, the works perfectly. 当我删除while(running)输出时,效果很好。

如果有帮助,请快速输入以下内容:1.以不稳定方式运行2.关闭PrintStream(不确定是否有帮助)

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

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