CODE‎ > ‎Python‎ > ‎

多執行緒

import time 
import sys
from random import randint
from threading import *

class testit(Thread):
    def __init__ (self,name,number):
        Thread.__init__(self)
        self.name= name
        self.number = number
    def run(self):
        print "Item:",self.name,":",id(self)
    def show(self):
        print "Item",self.name,"'s ID is:",id(self)
        print "Item",self.name,"'s Number is:",self.number

print time.ctime()
testits = []

for host in range(1,30):
    number = randint(1,10000)
    ship= str(host)
    star= testit(ship,number)
    star.start()
    testits.append( star )

print "waiting for threads..."
for t in testits:
    t.join()
#time.sleep(1)
print "完成"
Comments