1

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

Lele
  • 624
  • 2
  • 15
  • 31

3 Answers3

0

Have you tried with scan.close() or scanner.close()?

geek-tech
  • 95
  • 1
  • 9
0

This seems to be a case where using Thread.interrupt is a valid occasion. Once the processing is successful, do a stopChecker.interrupt - that should fail the Scanner.next with a ClosedByInterruptException exception. Note that according to javadoc System.in might be closed because of this operation.

If that is not an option, the only solution is to not to use Scanner and do your option read in a non-blocking way.

Dakshinamurthy Karra
  • 4,969
  • 1
  • 13
  • 25
0

Please change the code as follows.

1) Replace while loop with do - while block

2) Change Scanner code as follows

 do{
    while ( scanner.hasNext() ) {
         String command = scanner.nextLine();
         System.out.println("line:"+command);
    }
 } while ( conditionFlag)

When the condition is false, call

scanner.close(); 
Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
  • `scanner.hasNext()` is always true of the scanner reads from `System.in` and that stream isn't closed. – Tom Aug 26 '15 at 11:37
  • But I am closing the stream outside of while loop once the thread has been stopped – Ravindra babu Aug 26 '15 at 11:46
  • 1
    "once the thread has be stopped" and when should that happen if the thread stays in the `while` lopp? And since you're closing the scanner you also make it impossible to read more user data later. – Tom Aug 26 '15 at 11:50