![]() |
VOOZH | about |
Like running multiple independent programs simultaneously, running multiple threads simultaneously also has similar features with some added benefits, which are as follows :
Multi threading is defined as the ability to execute multiple threads simultaneously or concurrently. Hence more than one thread can exists in a single process where:
On invoking the join() method, the calling thread gets blocked until the thread object (on which the thread is called) gets terminated. The thread objects can terminate under any one of the following conditions:
Hence the join() method indicates wait till the thread terminates. We can also specify a timeout value to the join() method. In such a situation the calling thread may ask the thread to stop by sending a signal through an event object. The join() method can be called multiple times.
Syntax:
object_name.join() OR object_name.join(timeout) where timeout argument is the timeout value.
Example:
Output:
The Child Thread sleep count is 1 Main thread is starting to wait for 5 seconds The Child Thread sleep count is 2 Main thread says : I cant't wait for more than 5 seconds for the child thread; Will ask child thread to stop Asked to stop A Child Thread is exiting Main thread says : Now I do something else to compensate the child thread task and exit Main thread is exiting
Points to remember while joining threads using join() in Python: