python多线程学习

理解不深,先这样!

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


发布了25 篇原创文章 · 获赞 6 · 访问量 6223
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章