on my code, I've a long loop of sequential operation. I make a thread to stop this loop manually. Basically
private static void sendAndCheck() throws InterruptedException {
stop = false;
StopChecker r = new StopChecker();
Thread t = new Thread( r );
if ( vettore != null ) {
t.start();
for ( int i = 0; i < vettore.length; i++ ) {
do {
if ( stop == true ) {
break;
}
//Do something for a lot of time.. (code deleted)
} while ( !status.equalsIgnoreCase( "PROCESSED" ) ); //<- normal exit
}
//Stop the thread
stop = true;
}
}
and the thread basically is waiting on the standard input for stop character
public class StopChecker implements Runnable {
public void run() {
Scanner scanner = new Scanner( System.in );
System.out.println( "1 - to stop" );
while ( !Risottometti.stop ) {
String command = scanner.next();
if ( command.equalsIgnoreCase( "1" ) ) {
Risottometti.stop = true;
}
}
}
}
}
The problem is that, if the loop exit normally, the thread is locked on the scanner.next, so, the next input is lose inside the still dead thread. How to release the scanner.next from main class? I've tried with scan.close() but doesn't work...
There's another way to stop the loop, without kill the application? i try with keyListener, but i've got a null pointer