zoukankan      html  css  js  c++  java
  • Python 多线程实现循环打印 abc

    python 多线程实现循环打印 abc

    好久没写过python了, 想自己实践一下把

    非阻塞版

    import threading
    import time
    
    def print_a():
        global value
        global lock
        global stop_flag
        while stop_flag:
            while True:
                if value == 0 or value == 3:
                    break
            lock.acquire()
            value = 1
            time.sleep(1)
            print("aaa")
            lock.release()  
    
    def print_b():
        global value
        global lock
        global stop_flag
        while stop_flag:
            while True:
                if value == 1:
                    break
            lock.acquire()
            value = 2
            time.sleep(1)
            print("bbb")
            lock.release()
    
    def print_c():
        global value
        global lock
        global stop_flag
        while stop_flag:
            while True:
                if value == 2:
                    break
            lock.acquire()
            value = 3
            time.sleep(1)
            print("ccc")
            lock.release()
    
    if __name__ == "__main__":
        stop_flag = True
        value = 0
        threads = []
        lock = threading.Lock()
        thread_a = threading.Thread(target=print_a)
        thread_b = threading.Thread(target=print_b)
        thread_c = threading.Thread(target=print_c)
        threads.append(thread_a)
        threads.append(thread_b)
        threads.append(thread_c)
    
        for thread in threads:
            thread.start()
        time.sleep(5)
        stop_flag = False
    

    阻塞版

    import threading
    import time
    
    def print_a():
        global value
        global stop_flag
        global lock
        global con
        while stop_flag:
            try:
                lock.acquire()
                while value != 0 and value != 3:
                    con.wait()
                time.sleep(1)
                value = 1
                print("aaa")
                con.notify_all()
            finally:
                lock.release()
    
    
    def print_b():
        global value
        global stop_flag
        global lock
        global con
        while stop_flag:
            try:
                lock.acquire()
                while value != 1:
                    con.wait()
                time.sleep(1)
                value = 2
                print("bbb")
                con.notify_all()
            finally:
                lock.release()
        
    
    def print_c():
        global value
        global stop_flag
        global lock
        global con
        while stop_flag:
            try:            
                lock.acquire()
                while value != 2:
                    con.wait()
                time.sleep(1)
                value = 3
                print("ccc")
                con.notify_all()
            finally:
                lock.release()
    
    if __name__ == "__main__":
        stop_flag = True
        value = 0
        threads = []
        # 注意这里使用的是条件变量
        lock = threading.Lock()
        con = threading.Condition(lock=lock)
        thread_a = threading.Thread(target=print_a)
        thread_b = threading.Thread(target=print_b)
        thread_c = threading.Thread(target=print_c)
        threads.append(thread_a)
        threads.append(thread_b)
        threads.append(thread_c)
    
        for thread in threads:
            thread.start()
        time.sleep(5)
        print("stop")
        stop_flag = False
        for thread in threads:
            thread.join()
    
  • 相关阅读:
    响应式布局
    Fiddler2汉化版使用说明
    nonmember,nonfriend替换member函数
    Java回顾之Spring基础
    dudu,想在cnblogs首页看很久以前的文章不行。
    基于Nios II内核的项目程序为什么越优化越慢?
    学习 easyui:禁用 linkbutton 问题
    Socket编程 (异步通讯,解决Tcp粘包) 3
    .NET:可扩展的单据编号生成器 之 基于缓冲区的顺序号
    淘宝API应用开发
  • 原文地址:https://www.cnblogs.com/Draymonder/p/11967947.html
Copyright © 2011-2022 走看看