python 多线程与资源


import threading
import time

lock=threading._RLock()
words=["a","b","d","b","p","m","e","f","b"]

def increase():
    global words
    for count in range(5):
        lock.acquire()
        print("A acquired")
        for i in range(len(words)):
            for j in range(i+1,len(words)):
                if words[j]<words[i]:
                    t=words[i]
                    words[i]=words[j]
                    words[j]=t

        print('A',words)
        time.sleep(1)
        lock.release()

def decrease():
    global words
    for count in range(5):
        lock.acquire()
        print("D acquired")
        for i in range(len(words)):
            for j in range(i+1,len(words)):
                if words[j]<words[i]:
                    t=words[i]
                    words[i]=words[j]
                    words[j]=t

        print('D',words)
        time.sleep(1)
        lock.release()

A=threading.Thread(target=increase)
A.daemon=False
A.start()

D=threading.Thread(target=decrease)
D.daemon=False
D.start()
print("the End")

声明:木东蓝博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - python 多线程与资源


学习新思想,争做新青年!