代码:
#!/user/nim/python
# -*-coding: UTF-8 -*-
import thread
import time
def get_time( threadName, delay):
count = 0
while count < 3:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
try:
thread.start_new_thread( get_time, ("Thread-1", 2, ) )
thread.start_new_thread( get_time, ("Thread-2", 4, ) )
except:
print ("Error!")
while 1:
pass
运行结果:
Thread-1: Tue Oct 27 19:47:10 2020
Thread-2: Tue Oct 27 19:47:12 2020
Thread-1: Tue Oct 27 19:47:12 2020
Thread-1: Tue Oct 27 19:47:14 2020
Thread-2: Tue Oct 27 19:47:16 2020
Thread-2: Tue Oct 27 19:47:20 2020