在线程中运行时,while循环不起作用

因此,基本上,我试图让此while循环在此线程中运行,当“ activate”评估为true时,它应该激活,但由于某种原因它不起作用。 “ activate”是布尔值,当用户按下鼠标按钮时会激活(我为此设置了侦听器)。如果有人想知道我是否为此项目使用jnativehook库。任何帮助或解释将不胜感激。

private boolean activate;
private Robot robot;


@Override
public void run() {
    try {
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }

    while(true) {
        if (activate == true) {
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            robot.delay(100);
        }
    }       
}
charoto 回答:在线程中运行时,while循环不起作用

一种可能性是编译器(Java编译器或JIT编译器)已决定不需要测试activate,因为它可以证明while循环内的任何内容都不会改变它。在大多数已编译的编程语言中,除非您进行其他特殊说明,否则编译器可以假定为单线程代码。该假设是合理的,因为它使编译器在大多数时间可以生成效率更高的代码。

synchronized块中访问变量,或者将变量声明为volatile将阻止编译器做出该假设。

最好还是使用private final AtomicBoolean activate;

,

activate永远不会设置为true。 如果您不相信我,请在while循环的底部添加以下行:

System.out.println("activate = " + activate);

,
// Here is the sample program which should run your method correctly. All the changes 
// are commented upon. Don't judge harshly,not a professional with java


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.Scanner;

class Processor extends Thread {

// Have to initialize the variable here
private boolean activate = true;
private Robot robot;

public void run() {
    try {
        robot = new Robot();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // No need for while (true) here
    while (activate) {
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        robot.delay(100);
    }
}

// method to stop the program
public void shutdown() {
    activate = false;
}
}

public class App {
public static void main(String[] args) throws AWTException,InterruptedException,NullPointerException {
    // activates the process
    Processor p = new Processor();
    p.start();

    // Press any key to stop the process
    // program will run endlessly until scanner value (key input) is provided
    Scanner key_input = new Scanner(System.in);
    key_input.nextLine();
    p.shutdown();
}
}
,

感谢所有建议,通过将类实例传递给我的MouseListener类来解决此问题。

对于遇到相同问题的任何人:

public boolean activate;
public boolean toggled;

private Robot robot;
public MouseListener mouseListener = new MouseListener();
public KeyboardListener keyListener = new KeyboardListener();
public static Game instance;

public Game() {
    this.activate = false;
//  this.toggled = false;

    try {
        GlobalScreen.registerNativeHook();
        GlobalScreen.isNativeHookRegistered();
        GlobalScreen.addNativeMouseListener(mouseListener);
        GlobalScreen.addNativeKeyListener(keyListener);
    } catch (NativeHookException e) {
        e.printStackTrace();
    }
}

public void run() {
    try {
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }

    while(true) {
        try {
            Thread.sleep(1L);
            if(Game.this.isActivate()) {
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                robot.delay(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }       
}

public boolean isActivate() {
    return activate;
}

public void setActivate(boolean activate) {
    this.activate = activate;
}

public boolean isToggled() {
    return toggled;
}

public void setToggled(boolean toggled) {
    this.toggled = toggled;
} 

**public static Game getGame() {
    if(Game.instance == null) {
    Game.instance = new Game();
    }
    return Game.instance;
}** 

这里是将“激活”更改为“真”的类。

public void nativeMouseClicked(NativeMouseEvent e) {
    // nothing
}


public void nativeMousePressed(NativeMouseEvent e) {
    if(e.getButton() == NativeMouseEvent.BUTTON1) {
        Game.getGame().setActivate(true);
    }
}


public void nativeMouseReleased(NativeMouseEvent e) {
    if(e.getButton() == NativeMouseEvent.BUTTON1) {
        Game.getGame().setActivate(false);
    }
}

}

本文链接:https://www.f2er.com/3107240.html

大家都在问