Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threads join() question
#1
Hi guys,

so I'm pretty new to threading, I have a question in regards to the join function let me put some example code below

import threading

def funcOne():
     print(threading.current_thread().getName())

def funcTwo():
    print(threading.current_thread().getName())


t1 = threading.Thread(target = funcOne, name = "T one")
t2 = threading.Thread(target = funcTwo, name = "T two")

t1.start()
t2.start()

t1.join()
t2.join()

print("bye")
so my question is, I start both threads with t1.start() and t2.start(), so what does the join function do here? from what I have took in, the join() function will make sure that thread finishes before running anymore threads/ functions, BUT as you can see I call t1.join() so now does main stop functioning until t1 finishes? if so is t2 still running?? does the t2.join() function get called after the thread t1 finishes?

thanks
Reply
#2
Thread.join() will stop the current thread, until the referenced thread has completed. In your example, t1.join() would stop the main thread, while both t1 and t2 continue running. Once t1 completes, the main thread would resume. The very next line, t2.join(), would again halt the main thread until t2 had completed. At that point, you had already guaranteed that t1 had completed, so the only running thread would be t2.

Once t2 completes, the main thread would resume. At the next instruction, print("bye"), the main thread is the only thread still running.
Reply
#3
(Mar-19-2019, 07:59 PM)nilamo Wrote: Thread.join() will stop the current thread, until the referenced thread has completed. In your example, t1.join() would stop the main thread, while both t1 and t2 continue running. Once t1 completes, the main thread would resume. The very next line, t2.join(), would again halt the main thread until t2 had completed. At that point, you had already guaranteed that t1 had completed, so the only running thread would be t2.

Once t2 completes, the main thread would resume. At the next instruction, print("bye"), the main thread is the only thread still running.

thanks nilamo,

that makes sense, but lets suppose t2 somehow finished before t1, t1.join() finishes now the next line will be t2.join() but t2 has already finished running also, what would happen?

thanks
Reply
#4
Then t2.join() would immediately return. Calling .join() will halt the current thread, *if needed*. If that thread is already complete, then it's a do-nothing operation.
Reply
#5
thanks nilamo that makes sense :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,310 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Question on Join() function sduvvuri 2 2,714 Jun-02-2019, 03:55 PM
Last Post: perfringo
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,204 Feb-14-2019, 08:34 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020