'How to wait for one threading to finish then run another threading

I need to open multiple chrome drivers with selenium, then execute my script by threading in them. How to make it wait until first threading is finished and then start second threading. time.sleep(x) wont work for me, as I do not know how much time would first threading take and I need second threading to start as soon as first one is finished.

import time
import threading
from selenium import webdriver


mydrivers=[]
tabs = []
class ActivePool(object):
    def __init__(self):
        super(ActivePool, self).__init__()
        self.active = []
        self.lock = threading.Lock()
    def makeActive(self, name):
        with self.lock:
            self.active.append(name)
    def makeInactive(self, name):
        with self.lock:
            self.active.remove(name)
                    
def main_worker(s):
    #Driver State
    global tabs
    global mydrivers
    mydrivers.append(webdriver.Chrome())
    tabs.append(False)

def worker(s, pool):
        with s:
            global tabs
            global mydrivers
            name = threading.currentThread().getName()
            pool.makeActive(name)
            x = tabs.index(False)
            tabs[x] = True
            mydrivers[x].get("https://stackoverflow.com")
            time.sleep(15)
            pool.makeInactive(name)
            tabs[x]= False   



for k in range(5):
    t = threading.Thread(target=main_worker, args=(k,))
    t.start()

# How to make it wait until above threading is finished and then start below threading

pool = ActivePool()
s = threading.Semaphore(5)
for j in range(100):
    t = threading.Thread(target=worker, name=j, args=(s, pool))
    t.start()


Solution 1:[1]

thds = []
for k in range(5):
    thds.append( threading.Thread(target=main_worker, args=(k,)))
for t in thds:
    t.start()
for t in thds:
    t.join()

Or, even:

thds = [threading.Thread(target=main_worker, args=(k,)) for k in range(5)]
for t in thds:
    t.start()
for t in thds:
    t.join()

Solution 2:[2]

To wait for a thread to finish you should use the thread.join function. Eg...

from threading import Thread
import time

def wait_sec():
    time.sleep(2)

my_thread = Thread(target=wait_sec)
my_thread.start()
# after starting the thread join it to wait for end of target
my_thread.join()
print("You have waited 2 seconds")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts
Solution 2 Amitoj Singh Saini