Last active
June 15, 2021 06:50
-
-
Save rayepeng/7f9f3a7e43092eb3efbb10a22d5d7c05 to your computer and use it in GitHub Desktop.
多线程 #Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 调用join表示自身优先执行 | |
import threading | |
#定义线程要调用的方法,*add可接收多个以非关键字方式传入的参数 | |
def action(*add): | |
for arc in add: | |
#调用 getName() 方法获取当前执行该程序的线程名 | |
print(threading.current_thread().getName() +" "+ arc) | |
#定义为线程方法传入的参数 | |
my_tuple = ("http://c.biancheng.net/python/",\ | |
"http://c.biancheng.net/shell/",\ | |
"http://c.biancheng.net/java/") | |
#创建线程 | |
thread = threading.Thread(target = action,args =my_tuple) | |
#启动线程 | |
thread.start() | |
#指定 thread 线程优先执行完毕 | |
thread.join() | |
#主线程执行如下语句 | |
for i in range(5): | |
print(threading.current_thread().getName()) | |
# 结果: | |
''' | |
Thread-1 http://c.biancheng.net/python/ | |
Thread-1 http://c.biancheng.net/shell/ | |
Thread-1 http://c.biancheng.net/java/ | |
MainThread | |
MainThread | |
MainThread | |
MainThread | |
MainThread | |
''' | |
# 不使用join | |
import threading | |
#定义线程要调用的方法,*add可接收多个以非关键字方式传入的参数 | |
def action(*add): | |
for arc in add: | |
#调用 getName() 方法获取当前执行该程序的线程名 | |
print(threading.current_thread().getName() +" "+ arc) | |
#定义为线程方法传入的参数 | |
my_tuple = ("http://c.biancheng.net/python/",\ | |
"http://c.biancheng.net/shell/",\ | |
"http://c.biancheng.net/java/") | |
#创建线程 | |
thread = threading.Thread(target = action,args =my_tuple) | |
#启动线程 | |
thread.start() | |
#主线程执行如下语句 | |
for i in range(5): | |
print(threading.current_thread().getName()) | |
# 结果: | |
''' | |
Thread-1 http://c.biancheng.net/python/ | |
MainThread | |
Thread-1 http://c.biancheng.net/shell/ | |
MainThread | |
Thread-1 http://c.biancheng.net/java/ | |
MainThread | |
MainThread | |
MainThread | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
balance = 0 | |
def change_it_without_lock(n): | |
global balance | |
# 不加锁的话 最后的值不是0 | |
# 线程共享数据危险在于 多个线程同时改同一个变量 | |
# 如果每个线程按顺序执行,那么值会是0, 但是线程时系统调度,又不确定性,交替进行 | |
# 没锁的话,同时修改变量 | |
# 所以加锁是为了同时只有一个线程再修改,别的线程表一定不能改 | |
for i in range(100000): | |
balance = balance + n | |
balance = balance - n | |
threads = [ | |
threading.Thread(target=change_it_without_lock, args=(8,) ), | |
threading.Thread(target=change_it_without_lock, args=(10,) ) | |
] | |
[t.start() for t in threads] | |
[t.join() for t in threads] | |
print(balance) | |
# 不加锁输出结果: | |
''' | |
(py36) ➜ MultipleCameras python lock.py | |
0 | |
(py36) ➜ MultipleCameras python lock.py | |
0 | |
(py36) ➜ MultipleCameras python lock.py | |
0 | |
(py36) ➜ MultipleCameras python lock.py | |
8 | |
(py36) ➜ MultipleCameras python lock.py | |
0 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 意义不明的代码 | |
import threading | |
import time | |
def test(k): | |
for i in range(k): | |
print('test ',i) | |
# time.sleep(1) | |
thread = threading.Thread(target=test, args=(6,)) | |
thread.start() | |
for i in range(5): | |
print('main ', i) | |
# time.sleep(1) | |
# 打印结果,每次打印都不一样 | |
''' | |
test 0 | |
main 0 | |
test 1 | |
main 1 | |
main 2 | |
main 3 | |
main 4 | |
test 2 | |
test 3 | |
test 4 | |
test 5 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment