from threading import Thread, currentThread import time import itertools COUNT = 10000000 items = [] def count(n): name = currentThread().name while n > 0: n -= 1 items.append(name) t1 = Thread(target=count,args=(COUNT,)) t2 = Thread(target=count,args=(COUNT,)) t3 = Thread(target=count,args=(COUNT,)) t1.start(); t2.start(); t3.start() t1.join(); t2.join(); t3.join() # Output a condensed trace showing thread scheduling for key, seq in itertools.groupby(items): print("%s %s" % (key, len(list(seq))))