每日一记
多线程
cpu:中央处理器(CPU,英语:Central Processing Unit / Processor),是电子计算机的主要设备之一,电脑中的核心配件。其功能主要是解释计算机指令以及处理计算机软件中的数据。电脑中所有操作都由CPU负责读取指令,对指令译码并执行指令的核心部件。
程序:代码指令集合
进程:程序的执行过程
线程:指令执行的最小单位
线程安全 : 慢 (同步)
非线程安全(线程不安全): 快 (异步)
锁?
线程锁-互斥锁 Lock
lock.acquire() #获取锁
lock.release() #释放锁
函数方式实现多线程
继承方式实现多线程
线程的几种状态(生命周期):
创建状态------>就绪状态------>运行状态------>死亡状态
| |
|-挂起状态-|
#函数式实现多线程
import threading
def demo(name):
for i in range(10):
print(name,i)
t1 = threading.Thread(target=demo,args=("小宇",))
t2 = threading.Thread(target=demo,args=("小明",))
t1.start()
t2.start()
#同时进行三个线程,可以看出哪一个先结束
import threading
import time
def run(name):
for i in range(1,101):
print(name,"跑了",i,"米")
p1 = threading.Thread(target=run,args=("小红",))
p2 = threading.Thread(target=run,args=("小绿",))
p3 = threading.Thread(target=run,args=("小明",))
p1.start()
p2.start()
p3.start()
#下面代码不完善,加上延时后取出时存在列表为0时仍再取出,出现下标越界错误
import threading,time
list=[]
def cun(name):
while True:
list.append("方便面")
print(name,"存了一箱方便面,仓库剩余:",len(list))
time.sleep(1)
def qu(name):
while True:
if len(list)==0:
time.sleep(1)
else:
list.pop()
print(name,"取了一箱方便面,仓库剩余:",len(list))
time.sleep(1)
p1 = threading.Thread(target=cun,args=("p1",))
p2 = threading.Thread(target=cun,args=("p2",))
p3 = threading.Thread(target=cun,args=("p3",))
p4 = threading.Thread(target=qu,args=("p4",))
p5 = threading.Thread(target=qu,args=("p5",))
p6 = threading.Thread(target=qu,args=("p6",))
p7 = threading.Thread(target=qu,args=("p7",))
p1.start()
p2.start()
p3.start()
p4.start()
p5.start()
p6.start()
p7.start()
#继承式实现多线程
import threading
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
for i in range(100):
print(i)
t1 = MyThread()
t2 = MyThread()
t1.start()
t2.start()
#3个线程同时进行
import threading,time
list = []
for i in range(1, 101):
list.append(i)
class MyThread(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name = name
def run(self):
while len(list) > 0:
#time.sleep(0.1)
list.pop()
print(self.name,"取了一箱方便面,仓库剩余:", len(list))
t1 = MyThread("小明")
t2 = MyThread("小龙")
t3 = MyThread("小虎")
t1.start()
t2.start()
t3.start()
加锁之后的代码更安全了
在安全性方面更好但是速度会变慢
import threading,time
lock = threading.Lock()
list = ["货物"]
def get():
#获取锁
lock.acquire()
if len(list)>0:
time.sleep(1)
list.pop()
print("取走一份")
#释放锁
lock.release()
t1 = threading.Thread(target=get)
t2 = threading.Thread(target=get)
t3 = threading.Thread(target=get)
t1.start()
t2.start()
t3.start()
下面代码实现的是存货与取货并发执行
#仓储模型
import threading,time
list = []
lock1 = threading.Lock()
lock2 = threading.Lock()
class Mythread(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name=name
def run(self):
if self.name == "取货人":
while True:
self.qu()
time.sleep(1)
else:
while True:
self.cun()
time.sleep(1)
def cun(self):
lock1.acquire()
list.append("货")
print("存货,剩余:",len(list))
lock1.release()
def qu(self):
lock2.acquire()
if len(list)>0:
list.pop()
print("取货,剩余:",len(list))
lock2.release()
t1 = Mythread("存货人")
t2 = Mythread("存货人")
t3 = Mythread("取货人")
t4 = Mythread("取货人")
t5 = Mythread("取货人")
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()