Easy solution: He should just run a static analyzer on the code first to see if it will terminate before he executes it.
Edit: Why the down vote?
Still, this isn't the easy solution. The easy solution is to use a process and terminate it if it takes too long. Threads can't be reliably terminated, and especially not without accidentally ruining internal state. (what happens with locks? etc.)