
Please check out following documentation: Java Thread Deadlock and Livelock. This will make the main Thread wait for Thread t2 to finish before going on. Quick Fix: Just replace the t2.isInterrupted() method in your main thread with the t2.join() method. A good example would be Goldbach's conjecture, that any positive even integer greater than two can be represented by the sum of two primes. That is the reason for the infinite loop. Take a mathematical problem that is believed to be true for all positive integer's n, but hasn't been proven to be true for every n. If the halting problem is too obscure, think of it this way. So there you have it, a dead-code detector that returns only "There is dead code" or "There is no dead code" must sometimes yield wrong answers.
#Dead code java for loop code
If (quine_text) returns false, then the line ("Dead code!") won't be ever executed, so this program actually does have dead code, and the detector was wrong.īut if it returns true, then the line ("Dead code!") will be executed, and since there is no more code in the program, there is no dead code at all, so again, the detector was wrong. Quine_text = string.Format(quine_text, (char)34, quine_text) String quine_text = string.Format(quine_text, (char)34, quine_text) Well, let's take the classical proof of the undecidability of the halting problem and change the halting-detector to a dead-code detector! It may be possible to write algorithms for more complicated cases (like an algorithm that checks whether a condition is syntactically a contradiction and therefore will always return false), but still, that wouldn't cover all possible cases.

However, to improve readability and ensure that logic errors are resolved, dead code should. These are specific cases, and therefore it's possible to write an algorithm for them. Dead code is usually optimized out of a program during compilation. Or any statement after a return (within the same scope). For example, blocks that are dependent on conditions that can be determined to be false at compile time. If it doesn't, then you know that your program halts (gets to the last line, and then to your added line of code).Ĭompilers usually check for things that can be proven at compile-time to be dead. If your dead-code detector detects that this line is dead, then you know that the program does not halt. Simple: you add a line of code after the end of the program you want to check for halt. How do you transfer an algorithm for dead code into an algorithm for the Halting problem? Since that has been proven to be impossible, it follows that writing an algorithm for dead code is impossible as well. That is, if you find an algorithm that can detect dead code in any program, then you can use that algorithm to test whether any program will halt.


The Halting problem is reducible to the problem of finding dead code. You may be able to write such an algorithm for specific types of programs, but not for all programs. The dead code problem is related to the Halting problem.Īlan Turing proved that it is impossible to write a general algorithm that will be given a program and be able to decide whether that program halts for all inputs.
