0

I am executing a simple command where user gives the options(args) and I made a logic to get the args in Java program in which I am using wait() for particular time so that command will take that much minimum time to execute.I am saving some data in a file after that.

Within this time if the user wants to end the process ,should be able to stop the process smoothly by giving input like "exit" in the command prompt.

Please help.

5
  • So you want to be able to "interrupt" ;) the wait, right? Commented Nov 13, 2014 at 13:48
  • yes,when the user prompts to stop. Commented Nov 13, 2014 at 13:54
  • I second @LeffeBrune 's answer. But maybe you can use the interrupt (see above comment, it is a link) stuff anyway. Commented Nov 13, 2014 at 13:56
  • Problem is not interrupting the thread,how user can input some command when already one command is running? Commented Nov 13, 2014 at 13:58
  • He can't with one single thread. Commented Nov 13, 2014 at 14:00

1 Answer 1

1

The standard way of interrupting a command line program is by adding a Ctrl-C handler to your app:

Runtime.getRuntime().addShutdownHook(new Thread() {
  public void run() { 
    // cleanup logic here
  }
});

See this question for more details.

Since you insist. Here is an implementation when commands are executed in background threads. I hope the complexity of this example will deter you from implementing it:

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Shell {
  private static final int NUM_PARALLEL_COMMANDS = 5;
  private static final int SLEEP_DURATION = 1000;

  public static void main(String[] args) throws InterruptedException {
    ExecutorService executor = 
        Executors.newFixedThreadPool(NUM_PARALLEL_COMMANDS);
    try (Scanner scanner = new Scanner(System.in)) {
      String command = null;     
      int counter = 0;
      do {
        command = scanner.nextLine();
        switch (command) {
          case "DoStuff":
            executor.submit(NewDoStuffCommand(++counter));
            break;
        }
      } while (!command.equals("exit"));
    }

    executor.shutdownNow();
    executor.awaitTermination(1, TimeUnit.SECONDS);
  }

  private static Runnable NewDoStuffCommand(final int counter) {
    return new Runnable() {
      @Override 
      public void run() {
        try {
          for (int i = 0; i < 10; i++) {
            System.out.println(counter + ": Doing time consuming things...");
            Thread.sleep(SLEEP_DURATION);
          }
          System.out.println(counter + ": Finished.");
        } catch (InterruptedException e) {
          System.out.println(counter + ": Command interrupted :(");
          // do cleanup
          Thread.currentThread().interrupt();
        }
      }
    };
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i want to send stop signal when user inputs "exit"even before command finishes execution,How can i do that?
You'll need to run multiple threads, which will make your code unnecessarily complicated. Exactly the reason why all good command line utils rely on BREAK command aka Ctrl-C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.