I doubt it. Usually, the only robust way to do this is to run the untrusted code in a separate process, properly sandboxed.
Consider what happens if the untrusted code acquires some critical resource (e.g. a lock), and then gets killed because it's taking too long. The entire process will deadlock if anyone will try to acquire that same lock.