What Does This Thread Join Code Mean

What does it mean to join a thread?

This is how one thread waits for the completion of another thread!

A nice use case of join is - say for example the main() function/thread creates a thread and doesn't wait ( using join ) for the created thread to complete and simply exits, then the newly created thread will also stop!

Here is a nice explanation of Thread Management in general and Thread Join in particular! And here are some code snippets that show you some use cases of join and what happens when you don't use it!

Why is the word join is used for the Thread.join() method?

The word "join" comes from the Fork–Join model, where "fork" means to split the thread into multiple threads for parallel processing, and "join" means to wait for the parallel threads to complete their part, before continuing in a single thread.

Thread.join() explanation

t.join() causes the current thread to pause execution until t's thread terminates.

By doing this:

 t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();

there is no reason to use threads...

thead.join() use example:

Lets say you need to encrypt 3 files,
you want to use threads for faster processing time, and you want to know how much time it took:

int startTime = System.currentTimeMillis();
firstFileEncryptorThread.start();
secondFileEncryptorThread.start();
thirdFileEncryptorThread.start();

firstFileEncryptorThread.join();
secondFileEncryptorThread.join();
thirdFileEncryptorThread.join();

System.out.println(System.currentTimeMillis() - startTime );

How does this thread join() work as given?

t.start() starts the execution of the new thread, which will execute the code in t.run(). But the call to t.start() will return immediately; it won't wait for that thread to finish its execution of run(). After t.start() returns, the main thread runs t.run(). Finally, it'll wait for the t thread to finish, which is what t.join() does. This method will not return immediately, but will instead wait for the thread to finish. (As it happens, that's going to be very fast in this case -- but it could potentially take minutes, hours, or even forever.)

The control flow looks something like this:

Thread A
|
+- t.start() ---> starts Thread B
+- t.run() +- t.run()
| |
+- t.join() waits for B |
| to finish |
| \------> +- Thread B stops
+- t.doIt()
+- Thread A stops

What exactly does Thread.join() do in python? Is this incorrect usage of Thread.join()?

would the "calling thread" be whichever thread called the loadFunction function?

The calling thread is whichever thread called join. so t1.join() and t2.join() and t3.join() cause the main thread to block, and the join inside loadFunction would cause t3 to block, if map was not lazily evaluated.

how would I fix it?

Your joins inside loadFunction aren't executing because map does not execute any code until you iterate over it. As MaxNoe suggests, you should use an ordinary for loop instead.

def loadFunction(name, start, end, wait=[]):
""" wait should be a list of threads to wait for """
for t in wait:
t.join()
for number in range(start, end):
print("%s : %d" % (name, number))


Related Topics



Leave a reply



Submit