import threading import time class my_first_threading(threading.Thread): string="" def __init__(self,name,counter,Thread_id,string): threading.Thread.__init__(self) self.counter=counter self.name=name self.Thread_id=Thread_id self.string=string def run(self): print("<---"+self.name+"begin--->\n") print_string(self.name,self.string,self.counter) print("<---"+self.name+"out--->\n") def print_string(name,string,counter): for i in range(counter): print(str(i+1)+". "+name+" :"+string+"\n") time.sleep(0.2) def main(): start_time=time.time() string1=input("\n\n\n第一个进程需要打印的字符串:") time_1=int(input("进程需要运行的次数 time: ")) string2=input("\n第二个进程需要打印的字符串:") time_2=int(input("进程需要运行的次数 time: ")) print("\n\n\n\n") th1=my_first_threading("进程一",time_1,1314,string1) th2=my_first_threading("进程二",time_2,3344,string2) start1_time=time.time() th1.start() th1.join() print("\n\n||--->子线一程束,耗时 "+str(time.time()-start1_time)+"秒<---||\n\n") start2_time=time.time() th2.start() th2.join() print("\n\n||--->子线二程束,耗时 "+str(time.time()-start2_time)+"秒<---||\n") print("\n\n||--->主线程结束,耗时 "+str(time.time()-start_time)+"秒<---||\n\n") return 1 if __name__ == "__main__": main()