import time
import threading
def sing():
for i in range(5):
print("唱歌")
time.sleep(1)
def dance():
for i in range(5):
print("跳舞")
time.sleep(1)
def main():
t1 = threading.Thread(target=sing)
t2 = threading.Thread(target=dance)
t1.start()
t2.start()
if __name__ == "__main__":
main()
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/thread/__init__.py
唱歌
跳舞
跳舞
唱歌
跳舞
唱歌
跳舞
唱歌
跳舞
唱歌
Process finished with exit code 0
打印正在运行的多个线程
import time
import threading
def sing():
for i in range(5):
print("唱歌")
time.sleep(1)
def dance():
for i in range(10):
print("跳舞")
time.sleep(1)
def main():
t1 = threading.Thread(target=sing)
t2 = threading.Thread(target=dance)
t1.start()
t2.start()
# 输出有多少个线程正在执行
while True:
print(threading.enumerate())
if len(threading.enumerate()) <= 1:
break
time.sleep(1)
if __name__ == "__main__":
main()
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/thread/CurrentRunningThread.py
唱歌
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-1, started 13768)>, <Thread(Thread-2, started 20764)>]
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-1, started 13768)>, <Thread(Thread-2, started 20764)>]
唱歌
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-1, started 13768)>, <Thread(Thread-2, started 20764)>]
唱歌
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-1, started 13768)>, <Thread(Thread-2, started 20764)>]
唱歌
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-1, started 13768)>, <Thread(Thread-2, started 20764)>]
跳舞
唱歌
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-1, started 13768)>, <Thread(Thread-2, started 20764)>]
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-2, started 20764)>]
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-2, started 20764)>]
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-2, started 20764)>]
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-2, started 20764)>]
跳舞
[<_MainThread(MainThread, started 10032)>, <Thread(Thread-2, started 20764)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
[<_MainThread(MainThread, started 10032)>]
Process finished with exit code -1
通过继承的方式实现线程
import time
import threading
class MyThread(threading.Thread):
def run(self):
for i in range(3):
time.sleep(1)
self.login()
print("线程执行方法")
def login(self):
print("登陆")
def register(self):
print("取消注册")
if __name__ == "__main__":
t = MyThread()
t.start()
多线程共享全局变量
import threading
import time
g_num = 100
def test1():
global g_num
g_num += 1
print("in test1 g_number=%d..." % g_num)
def test2():
print("in test1 g_number=%d..." % g_num)
def main():
t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
time.sleep(1)
t2.start()
print("...........in main thread g_num=%d.." % g_num)
if __name__ == "__main__":
main()
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/thread/ThreadShare.py
in test1 g_number=101...
in test1 g_number=101...
...........in main thread g_num=101..
Process finished with exit code 0
多线程共享全局变量 args参数
import threading
import time
g_num = 100
def test1(t):
global g_num
g_num += 1
t.append(400)
print("in test1 g_number=%s..." % str(g_num))
print("in test1 tem=%s..." % str(t))
def test2():
print("in test1 g_number=%s..." % str(g_num))
tem = [100, 200]
def main():
t1 = threading.Thread(target=test1, args=(tem,))
t2 = threading.Thread(target=test2)
t1.start()
time.sleep(1)
t2.start()
print("...........in main thread g_num=%s.." % str(g_num))
print("...........in main thread tem=%s.." % str(tem))
if __name__ == "__main__":
main()
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/thread/ThreadShare.py
in test1 g_number=101...
in test1 tem=[100, 200, 400]...
in test1 g_number=101...
...........in main thread g_num=101..
...........in main thread tem=[100, 200, 400]..
Process finished with exit code 0
互斥锁
import threading
import time
g_num = 0
def test1(num):
global g_num
# 上锁,如果之前没有被上锁,那么此时,上锁成功
# 如果上锁之前已经被上锁了,那么此时会堵塞在这里,直到这个锁被解开为止
mutext.acquire()
for i in range(num):
g_num += 1
# 解锁
mutext.release()
print(".....in test1 g_num=%d...." % g_num)
def test2(num):
global g_num
mutext.acquire()
for i in range(num):
g_num += 1
mutext.release()
print("....in test2 g_num=%d..." % g_num)
# 创建一个互斥锁,默认是没有上锁的
mutext = threading.Lock()
def main():
t1 = threading.Thread(target=test1, args=(1000000,))
t2 = threading.Thread(target=test2, args=(1000000,))
t1.start()
t2.start()
time.sleep(5)
print("......in main g_num=%d" % g_num)
if __name__ == "__main__":
main()
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/thread/ThreadLock.py
.....in test1 g_num=1000000....
....in test2 g_num=2000000...
......in main g_num=2000000
Process finished with exit code 0
如果不加锁我们对全局变量的操作可能会导致数据混乱,如下
E:\python_project\NetWork\venv\Scripts\python.exe E:/python_project/NetWork/thread/ThreadLock.py
.....in test1 g_num=1234092....
....in test2 g_num=1278644...
......in main g_num=1278644
Process finished with exit code 0
网友评论