1.多线程:
import threading
import time
def worker():
print('I am thread')
t = threading.current_thread()
time.sleep(8)
print(t.getName())
new_t = threading.Thread(target=worker,name='liao thread') #在主线程里启用了新线程
new_t.start()
t = threading.current_thread()
print(t.getName())
执行结果:
I am thread
MainThread
liao thread
2.单线程
import threading
import time
def worker():
print('I am thread')
t = threading.current_thread()
time.sleep(8)
print(t.getName())
worker()
t = threading.current_thread()
print(t.getName())
执行结果:
I am thread
MainThread
MainThread