[CentralOH] Python threads

Steven Huwig steven_h at acm.org
Mon Aug 4 20:23:33 CEST 2008


Your global counter is the problem. You should give the thread its own
number before you start the thread.

import threading
import time
import random

class MyThread ( threading.Thread ):
  def __init__(self, num):
      threading.Thread.__init__(self)
      self.num = num

  def run ( self ):
     time.sleep(random.random()*10)
     print 'This is thread ' + str ( self.num ) + ' speaking.'
     print 'Hello and good bye.'

for x in xrange ( 20 ):
   MyThread(x).start()


More information about the CentralOH mailing list