diff --git a/ch2/threads_v0.py b/ch2/threads_v0.py index 7afd94e..6bdeaf7 100644 --- a/ch2/threads_v0.py +++ b/ch2/threads_v0.py @@ -7,6 +7,7 @@ global count count = 0 +# This argument is used to tell the thread how many iters to go, e.g. 1000 itersPerThread = int(argv[1]) threadNum = 0 @@ -17,6 +18,9 @@ def __init__(self, iters): Thread.__init__(self, name="Thread-%d" % threadNum); threadNum += 1 self.iters = iters + # Calling run creates a new virtual machine + # That virtual machine doesn't start running until I tell the thread to start + # Once it stops the virtual machine is destroyed def run(self): global count for i in range(self.iters): @@ -29,6 +33,8 @@ def delay(sec): workers = [Worker(itersPerThread).start(), Worker(itersPerThread).start()] +# Enumerate is a method of the Thread class. It returns a list of threads that are +# still running while len(enumerate()) > 1: delay(0.25) print("activeThreads=%s, count=%d" % (enumerate(), count))