Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ch2/threads_v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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))
Expand Down