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.
Full MainLoop() method for class containing Miner.
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. 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:
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();
, 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.