java – the instance is still running even after closing it by thread

Well, even after closing my instance, it is still running due to a thread that I created to call a while:

public void nativeKeyPressed (event NativeKeyEvent) {
System.out.println ("Key pressed:" + NativeKeyEvent.getKeyText (event.getKeyCode ());
if (event.getKeyCode () == NativeKeyEvent.VC_BACKQUOTE) {
keepPressingO = true;
new thread () {
@Override
public void run () {
test {
while (keepPressingO) {
Robot robot = new Robot ();
robot.keyPress (KeyEvent.VK_Q);
Thread.sleep (26);
robot.keyRelease (KeyEvent.VK_Q);
robot.keyPress (KeyEvent.VK_W);
Thread.sleep (16);
robot.keyRelease (KeyEvent.VK_W);
robot.keyPress (KeyEvent.VK_E);
Thread.sleep (26);
robot.keyRelease (KeyEvent.VK_E);
}

} capture (AWTException e) {
e.printStackTrace ();
} catch (InterruptedException e) {
// TODO Automatic generated catch block
e.printStackTrace ();
}
}
} .start ();

}

if (event.getKeyCode () == NativeKeyEvent.VC_ENTER) {
keepPressingO = false;
}

}

Could someone help me solve this situation?
How could I make the thread fall asleep when it comes out of time (and if it would return if it woke up?)

And how could I call this thread inside a window? Is it closed to kill him?