1.线程创建
1.1函数创建线程
def helloworld():
time.sleep(2)
print("hello world")
t = threading.Thread(target=helloworld)
t.start()
print("main thread")
1.2类创建线程
class HelloWorld(threading.Thread):
def __init__(self,id):
self.id = id
super(HelloWorld,self).__init__()
def run(self):
time.sleep(2)
print("thread %d helloworld" % self.id)
for i in range(5):
t = HelloWorld(i)
t.start()
print("main thread")
2.线程的方法
t.start() 启动线程活动
t.join() 保证当前线程执行完成以后,再执行其他线程。
import threading,time
count = 0
def adder():
global count
count = count + 1
time.sleep(0.1)
count = count + 1
threads = []
for i in range(100):
thread = threading.Thread(target=adder)
thread.