There is a similar question already posted on SO, however, the solution works only if the loop doesn't generate any output.
If instead I try to run while(true) System.out.print(1);
and hit the Ctrl+C afterwards, what happens is the terminal just freezes and it seems there is no way to kill it.
Is anything can be done besides closing and reopening the terminal?
Asked
Active
Viewed 745 times
2

Community
- 1
- 1

Alex Weitz
- 3,199
- 4
- 34
- 57
-
1Good question. The weird thing is that when trying to print "Hello", sometimes using CTRL+C does kill the loop, sometimes it doesn't. – Maroun Mar 14 '16 at 15:12
-
in another terminal you can kill -9 the process. – Taylor Mar 14 '16 at 15:13
-
Since the question has aged now. Yet trying the same on the latest release 9.0.1 jshell `Ctrl+C` works for me. – Naman Jan 01 '18 at 02:14
1 Answers
-2
You shouldn't have a loop like that. You should be releasing some time to the CPU by sleeping the active thread for a while with
while (true) {
// Do your processing stuff here.
// This line ensures that the active thread releases some time
// to the CPU, allowing closure of the application via Ctrl-C.
Thread.sleep(numberOfMillisecondsToSleepFor);
}
You can kill the process, with your existing code, from another terminal using kill, but you should really be preventing that CPU locking scenario by coding the loop correctly to cater for it.

ManoDestra
- 6,325
- 6
- 26
- 50
-
3This doesn't really answer the question. Having an empty loop doesn't cause the same thing. – Maroun Mar 14 '16 at 15:17
-
An infinite loop `while (true)` is bad and should not be done unless some time is released to the CPU. His loop above does not do this. And that's why he can't kill the process easily. The problem is his loop code, not the subsequent symptoms of it. – ManoDestra Mar 14 '16 at 15:22
-
1Then what's the difference between an empty infinite loop and an infinite loop with a print statement (in terms of CPU usage)? – Maroun Mar 14 '16 at 15:23
-
Makes no difference what else you put in the loop. You still require to sleep the thread inside an infinite loop. Whatever else you put in there is up to you. I've updated the answer to make this clearer. – ManoDestra Mar 14 '16 at 15:24
-
1That's what I'm saying.. If you write an empty loop in the JShell, you **can** terminate it (only the loop, not the JShell process - without having any `sleep` methods there) using CTRL + C. It make no sense for me why having a print statement inside it changes this behavior. – Maroun Mar 14 '16 at 15:28
-
And what use is an empty infinite loop with no code within it? None. Therefore, it doesn't answer his question. You don't write code that doesn't release resources to the CPU. It's as simple as that. And if you were to stupidly type a single line infinite loop into a JShell prompt that doesn't allow for sleeping of the thread, then you can't guarantee any subsequent closure of the terminal. The only way to do that is to write infinite loops with great care. And doing as I've stated above by ensuring that CPU time is freed up. – ManoDestra Mar 14 '16 at 15:32
-
1You're not getting my point. I'm **very** aware that the code is useless. I'm trying to understand why having an empty infinite loop is different than having an infinite loop with an instruction, **in terms of JShell**. It **does** allow terminating an empty infinite loop, but not a one with a statement inside it. – Maroun Mar 14 '16 at 15:34
-
I'm guessing it ignores the infinite loop if it contains no instructions within it, but TBH that's quite irrelevant to the answer the OP requires to solve his problem here. – ManoDestra Mar 14 '16 at 15:38
-
I think OP is only having this as an example of potential loops that can go wrong (`while (true) { .. }`) is just an example. – Maroun Mar 14 '16 at 15:40
-
He doesn't state that at all. It merely states regarding an infinite loop scenario. And it's perfectly understandable why it will freeze up given the code he's posted. – ManoDestra Mar 14 '16 at 15:41
-
@ManoDestra so the answer is that nothing can be done? Anyway, I just wanted to play with JShell and test its behavior, see what happens in different scenarios. Writing infinite loops is not something I would do on purpose. – Alex Weitz Mar 15 '16 at 08:28
-
No, the answer is that you write while(true) loops with great care, releasing CPU resource as required. Or you can kill the process from another terminal, if you accidentally create one that you can't get out of. I never said at any point that there was no solution. I gave this answer above quite fully, I think. But if you write an infinite loop that doesn't allow for CPU handling of its closure, then that's all that's left to you, I'm afraid. – ManoDestra Mar 15 '16 at 12:57
-
1This indeed does *not* answer the question. The fact is that jshell was created to test Java code. It's very possible to create an infinite loop just by testing out certain loop constructs. You would want an option within `jshell` to break off execution in that case. This was not about the best way of programming a loop that can be stopped; that *certainly* wasn't asked for. – Maarten Bodewes Apr 06 '16 at 15:24
-
Fixing JShell's issues is not the solution here. The issue is by asking JShell to run an obvious infinite loop. Now, there's a number of ways round that. a) JShell could allow you to kill a long running process. There's nothing we can do about that. b) You could kill the process from a separate console. Or, c), perhaps, just perhaps, you kill the problem at its source and learn to not write infinite loops that do not release resource to the CPU. Which is the right solution to this obvious issue of running poorly constructed code in an immediate mode Java tool that allows such erroneous action. – ManoDestra Apr 06 '16 at 15:44