import threading import datetime import time, random class MyThread(threading.Thread): def __init__(self, id): self.id = id self.delay = random.random() threading.Thread.__init__(self) def run(self): time.sleep(self.delay) now = datetime.datetime.now() print("ID => %s: %s completes at %s\n" % \ (self.id, self.getName(), now)) # Start threads threads = [] for i in range(10): threads.append(MyThread(i)) threads[i].start() print('Active threads: ' + str(threading.activeCount())) # Print threads print('All threads: \n') print(threading.enumerate()) # Wait for all threads to complete for t in threads: t.join() print('Threads complete')