简体   繁体   English

Java-线程不会停止

[英]Java - Thread Doesn't Stop

I have the following class that's nested in another class (that also extends thread) 我有一个嵌套在另一个类中的以下类(它也扩展了线程)

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();
    }
}

Then in the class that Miner is nested in I have 然后在矿工嵌套的课程中

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

However, after my attempt at setting running to false and stopping the miner thread and doing "do more stuff", that whole time I am doing "do more stuff" it is still pressing the D key. 但是,在尝试将运行设置为false并停止矿工线程并执行“做更多的事情”之后,我一直在做“做更多的事情”时,仍然按D键。

Full MainLoop() method for class containing Miner. 包含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();
    }

The goal is basically to have the Miner thread press down the D key until it detects a certain color on a point on my screen. 基本上,目标是使Miner线程按下D键,直到它检测到屏幕上某个点上的某种颜色。 Then it should stop the miner thread while it moves my character a bit and then re-call the method and start all over again. 然后它应该在移动我的角色的同时停止矿工线程,然后重新调用该方法并重新开始。 But the miner thread runs the whole time. 但是矿工线程一直运行。

There is no stop conidion here: 这里没有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();
}

The method at the end calls itself => MainLoop(); 最后的方法调用自身=> MainLoop(); , this is a recursive infinite loop. ,这是一个递归无限循环。
In this method a new Miner object is crated, a new thread is started, D is pressed again, then the thread is stopped, then ...do more stuff.. and then the whole cycle starts again. 在这种方法中,创建一个新的Miner对象,启动一个新线程,再次按下D ,然后停止线程,然后...do more stuff..然后整个循环再次开始。

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

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