You won't be able to magically make the running thread stop after 30 seconds; what you would need to do is make that thread check some state after the time is up, notice it's taken too long and return
from its run method.
There are roughly two approaches to doing this:
- Have the library call thread perform its operation in "chunks" that take small amounts of time each, and check the status between each chunk. If you're processing hundreds of thousands of records, for example, you might process a few hundred in one batch then check the flag. Or if you're making potentially blocking calls with a timeout available, set the timeout to 500ms and check the flag between each call.
- Have the main thread interrupt the library call thread when the time is up, and ensure that the library thread handles interrupts correctly and checks the flag when this is the case. This would require the library call thread performs an interruptable method, though any potentially long-running method should allow for interrupts.
In both cases, you'd need to have one of the threads (probably the main thread as it has nothing else to do) set the "stop" flag to true
once the time is up (and this variable should be declared volatile so updates are seen by all threads). Likewise, when the library call thread sees this flag as true, it should stop what it's doing, generally by returning from the run() method.
You shouldn't really kill the library thread from the main thread; see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated for the reasons, and an alternative that looks very much like what I've described above. :-)