Skip to content Skip to sidebar Skip to footer

Multithreading In Python: When Does A Thread Become Inactive?

This is my code and I am getting varied output. import threading import time def th_fun(limit,sleeptime): print '%s' % (limit) print '%s number of threads are active' % (t

Solution 1:

The output is varied because the order that multiple threads are executed in, and the speeds of each of them, is nondeterministic.

As soon as t.start() occurs, the function gets called with the arguments you provided. However, this is running in the background while the next thread is set up and started. Depending on the amount of time it takes to run, it might finish running before the next thread starts, or it could finish after all the other threads have started.

It might help your understanding to add the line:

print"Finished thread %s" % limit

at the end of your function.

Post a Comment for "Multithreading In Python: When Does A Thread Become Inactive?"