python新人小白,希望各位大佬多指点!
work1.py
import threading
class Buffer1(object):
def __init__(self):
# 缓冲区数据
self.value = 0
# 缓冲区状态
self.empty = True # True缓冲区为空 False缓冲区为满
# 多线程的状态
self.con = threading.Condition()
#线程1上传信息
def update1(self):
self.con.aquire()
name=threading.current_thread().name
if self.empty:
self.value=value
self.empty=False
print('%s运行中'%name)
self.con.notifyAll() #通知线程2可以运行了
else:
self.con.wait() #线程2继续等待线程1完成
#线程2下载信息
def download2(self):
self.con.acquire()
name=threading.current_thread().name
ret=0
if self.empty: #缓冲区为空
self.con.wait() #等待线程1运行完成
else:
ret=self.value
self.empty=True
print('%s运行中'%name)
self.con.notifyAll() #通知线程3可以运行了
return ret
class Buffer2(object):
#线程2上传信息
def update2(self):
self.con.aquire()
name=threading.current_thread().name
if self.empty:
self.value=value
self.empty=False
print('%s运行中'%name)
self.con.notifyAll() #通知线程3可以运行了
else:
self.con.wait() #线程3继续等线程2
#线程3下载信息
def download3(self):
self.con.acquire()
name=threading.current_thread().name
fei=0
if self.empty: #缓冲区为空
self.con.wait() #等待线程2运行完成
else:
fei=self.value
self.empty=True
print('%s运行中'%name)
self.con.notifyAll() #通知线程1可以运行了
return fei
class Buffer3(object):
#线程3上传信息
def update2(self):
self.con.aquire()
name=threading.current_thread().name
if self.empty:
self.value=value
self.empty=False
print('%s运行中'%name)
self.con.notifyAll() #通知线程1可以运行了
else:
self.con.wait() #线程1等待
#线程1下载信息
def download1(self):
self.con.acquire()
name=threading.current_thread().name
qiu=0
if self.empty: #缓冲区为空
self.con.wait() #等待线程2运行完成
else:
qiu=self.value
self.empty=True
print('%s运行中'%name)
self.con.notifyAll() #通知线程1可以运行了
return qiu
work_main.py
import threading
from work1 import *
import time
import random
class MyThread1(threading.Thread):
def __init__(self,work1,name):
super().__init__()
self.work1=work1
self.name=name
def run1(self):
#不断的运行
while True:
time.sleep(1)
ret=self.work1.download2(value)
class MyThread2(threading.Thread):
def __init__(self,work1,name):
super().__init__()
self.work1=work1
self.name=name
def run1(self):
while True:
time.sleep(1)
fei=self.work1.download3()
class MyThread3(threading.Thread):
def __init__(self,work1,name):
super().__init__()
self.work1=work1
self.name=name
def run1(self):
while True:
time.sleep(1)
qiu=self.work1.download1()
def main():
work1=Buffer1
work1=Buffer2
work1=Buffer3
thread1=MyThread1(work1,'线程1')
thread1.start()
thread2=MyThread2(work1,'线程2')
thread2.start()
thread3=MyThread3(work1,'线程3')
thread3.start()
if __name__=='__main__':
main()
运行了一遍发现运行不出来,我这是哪儿出问题了,,哇,也没报错,这很郁闷啊!!!
大伙儿有什么方案吗?