0

Is there a way to interupt a java program while running(No Command line keys like Ctrl-C).

For Example I executed a program which runs an infinite loop printing "hello" and the only condition to stop is to press a key "E" but I do not want loop to pause waiting for the user to press the key.

I hope you can understand what i want.

R30B
  • 23
  • 3
  • taskmanager? kill? taskkill? – maio290 Jul 18 '20 at 15:38
  • 1
    Does this answer your question? [How to read a single char from the console in Java (as the user types it)?](https://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) – Progman Jul 18 '20 at 15:39
  • You can reach that result with 2 threads: the main thread starts the other thread and waits for user input. The other thread performs what it does, and it is shut-down by the main thread in case the user presses the key. However, this seems to be a workaround to a bug. If your program goes into an infinite loop, there is a bug in your code. You should rather fix the bug than implementing a complex way of stopping the loop without killing the main thread. – Matteo NNZ Jul 18 '20 at 15:54
  • @maio290 Taskill Actually i only want to get rid of that infinite loop by pressing the key without waiting for the user to input and continue the program – R30B Jul 18 '20 at 17:30
  • @MatteoNNZ My main aim is to stop the infinite loop(not a bug) by pressing the key and the loop should not wait for the user to inout and then enter. – R30B Jul 18 '20 at 17:32
  • @Progman It did not fully gave my answer but yes it did give me clarity for what i want – R30B Jul 18 '20 at 17:34

0 Answers0