How can return a variable from a thead (I have the threads handle too). Static variables will not work in this case.
Update: Here is one twist, how can I do this without having to block and wait for the result? I need to be able to poll the created thread and kill it if it is hanging for too long (eg> 1 minute), then continue on in the main thread if the spawned thread is taking too long.
Use
Callable<V>instead ofThread(orRunnable) so that you can get result asFuture<V>and useExecutorServiceto invoke it.Here’s an SSCCE, just copy’n’paste’n’run it:
Update: as per your update with the question how to kill it after a timeout, make use of
ScheduledExecutorServiceinstead. Here’s the slightly changed code: