Warning: These programs are clone bombs (a sort of less-dangerous but still-dangerous form of a fork bomb); as such, do not run them on a production system without sandboxing or resource limits. Clone bombs create threads in a loop (as opposed to fork bombs, which create processes in a loop), thus you can stop them simply by killing the process in question (making them much less dangerous than fork bombs, which can be very hard to clear up); but they will likely tie up most of your CPU until you manage to do so (or until the program wins and exits naturally by itself). Asking your OS to place limits on the amount of memory and CPU time these programs are allowed to use should create a safe environment in which to test them.
Java (OpenJDK 8), 65 60 bytes (with a slight modification to the wrapper)
Thread x=Thread.currentThread();new Thread(x::stop).start();
Try it online!
Requires both instances of catch (Exception …)
in the question to be changed to catch (Throwable …)
. This should in theory be more secure, not less, but it enables this solution to be possible.
I saved 5 bytes over the first version of this answer via using a method reference rather than a lambda.
Java 4, 104 bytes (untested, should work with the original wrapper)
final Thread x=Thread.currentThread();new Thread(){public void run(){x.stop(new Exception());}}.start();
Try it online! (link goes to a Java 8 implementation, thus won't work)
Using features that have been removed from modern versions of Java, it's possible to solve even the version of the puzzle which requires an Exception
. Probably, at least. (Java 4 is very old by now and I can't remember which features it did and didn't contain. As can be seen, there were many fewer features in Java back then and it was therefore more verbose; we didn't have lambdas, so I had to create an inner class.)
Explanations
Most of the solutions to this question are in C# (together with a Java solution that cheats via using unbalanced brackets as a form of code injection, and a Perl solution which also isn't in Java). So I thought it would be worth trying to show how this puzzle can be solved "properly" in Java too.
Both programs are effectively identical (thus the fact that the first program works gives me high confidence that the second program would work too, unless I've accidentally used a non-Java-4 feature; Thread#stop
was deprecated in Java 5).
Java's Thread#stop
method works, behind the scenes, via causing a throwable to be thrown into the thread in question. The throwable intended for the purpose is ThreadDeath
(an Error
, specifically because people often try to blanket-catch exceptions and Java's designers didn't want that happening), although it allows you to throw anything (or used to; at some point after the API was designed, Java's designers realised that this was an incredibly bad idea and removed the version of the method that takes arguments outright). Of course, even the version which throws ThreadDeath
is a fairly risky operation about which you can make few guarantees (for example, it allows you to solve this puzzle, something which "shouldn't" be possible), so you're not supposed to use it, but as of Java 8, it still works.
This program works by spawning a new thread, and asking it to forcibly throw an exception back into the main thread. If we're lucky, it'll do that at a point in time when we're outside the inner catch
block (we can't escape the outer catch
block until the program ends, because there's a loop around it). Because we have the loop conveniently added already, it's byte-saving to simply use that loop to allow us to keep creating threads, in the hope that one of them will eventually hit the correct timing. This normally seems to happen within a couple of seconds.
(TIO note: the current version of TIO is quite inclined to kill this program early in its execution, presumably due to all the threads being created. It can work on TIO, but does not work reliably, so often a few attempts are required to get the "You won!" output.)