#-*-coding:utf-8-*- fromTimeImportCtime,sleepImportThreading,datetime fromQueueImportQueueclassPDC (threading. Thread):def __init__(self,t_name): Threading. Thread.__init__(self,name=t_name)#self.name= ' aaa ' #此时self还不是Thread, for string format defRun (self):#The Run () method inherits from threading and needs to be rewritten to define its own contentSelf.setname ('b'+str (i))#self.setname (' BBB ') #此时self是Thread, you can see the properties/methods you have with Print dir (self) Print '%s:%s is producing%d to the queue.'%(CTime (), Self.getname (), i) Sleep (1)if __name__=='__main__': Threads=[] forIinchRange (5): T= PDC ('P'+str (i)) T.start () threads.append (t) forTinchThreads:t.join ()
return Result:
Fri APR 17:19:22 2016:B1 is producing 1 to the queue.
Fri APR 17:19:22 2016:B1 is producing 1 to the queue.
Fri APR 17:19:22 2016:B2 is producing 2 to the queue.
Fri APR 17:19:22 2016:B4 is producing 4 to the queue.
Fri APR 17:19:22 2016:B4 is producing 4 to the queue.
Multithreaded Applications-Class (thread)