python多线程学习

人盡茶涼 提交于 2020-01-03 03:57:14

理解不深,先这样!

# -*- 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()


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!