0

The following working example prints every 3sec a integer between 1 and 20. The program should terminate if one of the two status flacs (t1.getStatus, t2.getStatus) are true. If t1.getStatus is true then the program is still running, because the scanner doesn't terminate.

public class Test {

    public static void main(String[] args) {

        FirstTask t1 = new FirstTask();

        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        executor.scheduleAtFixedRate(t1, 0, 3, TimeUnit.SECONDS);

        ParallelScanner t2 = new ParallelScanner();
        Thread scan = new Thread(t2);
        scan.start();

        while(true){
            if(t1.getStatus() || t2.getStatus()) break;
        }
        scan.interrupt();
        executor.shutdown();
    }

}

class ParallelScanner implements Runnable {

    private volatile boolean status=false;

    public void run() {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        System.out.println("scanner stoped");
        scan.close();
        this.status=true;
    }

    public boolean getStatus(){
        return this.status;
    }

}

class FirstTask implements Runnable {

    private volatile boolean status=false;

    public void run() {
        this.status = status();
    }

    private boolean status() {
        int incident = (int) ((Math.random()*20)+1);
        System.out.println(incident);
        return incident < 7 ? true : false;
    }

    public boolean getStatus() {
        return this.status;
    }

}

To control the interrupt via status flac could be also the wrong scratch. I also found a similar question here. Till now the first answer doesn't grab me. And the second answer is wrong. Could someone provide a small working example on this approach? Are there some other alternatives?

Community
  • 1
  • 1
Hölderlin
  • 424
  • 1
  • 3
  • 16

1 Answers1

0

I guess you know that your FirstTask only ever runs once, so that's ok.

You need to make your status variable volatile to have it read from ram by every thread, or you will get kinda undefined behaviour.

Why do you have no loop that uses the scanner? I do not know the specifics of your Scanner class, is there an exception thrown when there is no more data, or null returned as my code assumes?

for(;;){
    String input = scan.nextLine();
    if(input == null)
         break;
    // Assuming that by jump over (run through) you mean:
    // ignore the line that comes from the scanner
    if(!t1.getStatus()){
        System.out.println(input); 
    }
}

also your getStatus() method needs to reset the status to true or it will also ignore (= not print) all consecutive lines.

John Smith
  • 2,282
  • 1
  • 14
  • 22
  • Thx for the keyword volatile. I rearranged the code and hopes that makes my question more understandable, because the issue is still the same. It prints every 3sec a number between 1-20. This is done by the executor. And the scanner is wrapped in a separate thread. But if the `t1.getStatus` is now true, the program is not terminated although the endless loop breaks. Because the scanner is waiting for input. – Hölderlin Feb 19 '17 at 12:38