理解不深,先這樣!
# -*- coding: utf-8 -*-
# usr/bin/python3.6.7
# @idea :PyCharm
# @FileName :moreTh.py
# @Time :2020/1/2 10:59
# @Author :zzq
import time
from threading import Thread, Lock
from queue import Queue
class ThOne(Thread):
def __init__(self, obj,que):
super(ThOne, self).__init__()
self.count = que
self.obj = obj
def run(self):
print("線程一開始")
while True:
num = self.count.get()
num += 1
self.count.put(num)
time.sleep(2)
print(num, self.name)
if num > 10:
break
print("線程一運行完成")
class ThTwo(Thread):
def __init__(self, obj, count):
super(ThTwo, self).__init__()
self.count = count
self.obj = obj
def run(self):
print("線程er開始")
while True:
num = self.count.get()
num += 1
self.count.put(num)
time.sleep(2)
print(num, self.name)
if num > 10:
break
print("線程二運行完成")
if __name__ == "__main__":
count = Queue()
count.put(0)
l = Lock()
l1 = Lock()
ThOne( l,count).start()
ThTwo(l1,count).start()