Java - 线程不会停止

时间:2016-06-05 20:48:09

标签: java multithreading key awtrobot

我有以下类嵌套在另一个类中(也扩展了线程)

public class Miner extends Thread {
    private volatile boolean running = true;

    public void setRunning(boolean running) {
        this.running = running;
    }
    public boolean getRunning() {
        return running;
    }

    private void MainLoop() {
        if(!running) {
            robot.keyRelease(KeyEvent.VK_D);
            return;
        }
        robot.keyPress(KeyEvent.VK_D);
        robot.keyRelease(KeyEvent.VK_D);
        try {
            Thread.sleep(100 + (int)(Math.random()*randInt));
        } catch(Exception e) {}

        MainLoop();
    }

    @Override
    public void run() {
        MainLoop();
    }
}

然后在Miner嵌套的类中我有

private void MainLoop() throws AWTException {
    Miner miner = new Miner();
    miner.start();
    ... does other stuff ...
    while(miner.getRunning())
        miner.setRunning(false);
    ... do more stuff ...
    MainLoop();
}

然而,在我尝试将运行设置为false并停止miner线程并执行“do more stuff”之后,我一直在做“做更多的事情”,它仍然按下D键。
<登记/> 包含Miner的类的完整MainLoop()方法。

private void MainLoop() throws AWTException {
        if(stop)
            return;
        Miner miner = new Miner();
        miner.start();
        while(true) {
            BufferedImage screenCap = robot.createScreenCapture(new Rectangle(x, y, 1, 1));
            int c = screenCap.getRGB(0,0);
            int  red = (c & 0x00ff0000) >> 16;
            int  green = (c & 0x0000ff00) >> 8;
            int  blue = c & 0x000000ff;
            Color color = new Color(red,green,blue);
            if(color.equals(DRILL_COLORS[0]) || color.equals(DRILL_COLORS[1]))
                break;
            try {
                Thread.sleep(500);
            } catch(Exception e) {}
            if(stop)
                return;
        }
        while(miner.getRunning())
            miner.setRunning(false);
        count++;
        if(count >= 3) {
            up = !up;
            count = 0;
        }

        switch(moveDir) {
            case 0:
                if(up)
                    holdKey(KeyEvent.VK_UP, 100 + (int)(Math.random()*randInt));
                else
                    holdKey(KeyEvent.VK_DOWN, 100 + (int)(Math.random()*randInt));
                break;
            case 1:
                if(up)
                    holdKey(KeyEvent.VK_RIGHT, 100 + (int)(Math.random()*randInt));
                else
                    holdKey(KeyEvent.VK_LEFT, 100 + (int)(Math.random()*randInt));
                break;
        }

        switch(wallLoc) {
            case 0:
                holdKey(KeyEvent.VK_UP, 50);
                break;
            case 1:
                holdKey(KeyEvent.VK_DOWN, 50);
                break;
            case 2:
                holdKey(KeyEvent.VK_RIGHT, 50);
                break;
            case 3:
                holdKey(KeyEvent.VK_LEFT, 50);
                break;
        }

        MainLoop();
    }

目标基本上是让Miner线程按下D键,直到它检测到屏幕上某个点上的某种颜色。然后它应该停止矿工线程,同时稍微移动我的角色,然后重新调用方法并重新开始。但矿工线程一直运行。

1 个答案:

答案 0 :(得分:2)

此处没有stop分隔符:

private void MainLoop() throws AWTException {
    Miner miner = new Miner();
    miner.start();
    ... does other stuff ...
    while(miner.getRunning())
        miner.setRunning(false);
    ... do more stuff ...
    MainLoop();
}

最后的方法称自己为=&gt; MainLoop();,这是一个递归的无限循环 在此方法中,创建一个新的Miner对象,启动一个新线程,再次按下D,然后停止线程,然后...do more stuff..,然后再次开始整个循环。 / p>