""" A simple threading example. Two threads execute in parallel. One prints out "a", the other prints out "1" """ from threading import Thread import time class SayStuffThread(Thread): def __init__(self, stuff_to_say, how_many_times): Thread.__init__(self) # initialize the parent class self.stuff_to_say = stuff_to_say self.how_many_times = how_many_times def run(self): number = 0 while(number < self.how_many_times): print(self.stuff_to_say) number += 1 time.sleep(1) def execute_in_parallel(n): thread1 = SayStuffThread("a", n) thread2 = SayStuffThread("1", n) thread1.start() thread2.start() # wait for threads to finish start_time = time.time() thread1.join() thread2.join() end_time = time.time() print(str(end_time - start_time) +" seconds") if __name__ == "__main__": execute_in_parallel(5) def show_variability(): outcomes = {} for i in range(10): test_outcome = test(10000) if test_outcome in outcomes: outcomes[test_outcome] += 1 else: outcomes[test_outcome] = 1 print outcomes