yes, in this last example the thread doesn't use join it will wait for the maximum amount of threshold time, as long as at least one thread is still alive. join normally means wait forever until the thread finishes. the test for isalive() will return false when all threads are completed and the waiting would stop.
this also works for all threads, (but not in linear ordering like each thread 20 seconds).
all threads are started at the same time, and run at the same time, up to the maximum threshold time, and any thread(s) that are still running at that point would be killed after that wait threshold.
But if all threads are found to finish before the threshold time, the main loop stops waiting (its possible then the total wait time would be less than 20 seconds if all threads finished before)
now this sample thread monitoring and kill it after a time threshold, assumes all threads were a constant number of threads, and all started at the same time.
its likely better to have a single thread to be in charge of monitoring and killing all threads in your application, where when each thread is created, and when it is started, it would be registered with this single monitor thread, and the montor thread would keep a "total execution time" for each of individual threads it is monitoring, and do the kill of that thread after its defined threshold (by this logic each thread could have its own different timeout threshold too). since the monitor thread would never exit, the trick would be to have your threads register with it, and unregistered from it when the thread finishes itself, or when the monitor thread kills a long running / hung thread.
|