日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python線程通信Condition提供的方法

1、acquire調(diào)用condition關(guān)聯(lián)的方法。

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、三河ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的三河網(wǎng)站制作公司

Lock的acquire()或release()。

2、wait傳入timeout參數(shù)。

指定該線程最多等待多少秒。

導(dǎo)致當(dāng)前線程進(jìn)入Condition的等待池等待通知并釋放鎖,直到其他線程調(diào)用該Condition的notify()或者notify_all()方法來喚醒該線程。在調(diào)用該wait()方法時可以

3、notify喚醒Condition的單個線程并通知。

收到通知的線程會自動調(diào)用accquire()方法嘗試加鎖。如果所有線程都在該Condition等待池中等待,則會選擇喚醒其中一個線程,選擇是任意性的。

4、notify_all喚醒所有線程并通知。

實例

import threading
class Account:
    # 定義構(gòu)造函數(shù)
    def __init__(self, account_no, balance):
        self.account_no = account_no
        self._balance = balance
        self.condition = threading.Condition()
        # 定義代表是否已經(jīng)存錢的標(biāo)識
        self.__deposit_flag = False
 
    # 取錢
    def draw(self, draw_amount):
        # 加鎖
        self.condition.acquire()
        try:
            # 還沒存錢
            if not self.__deposit_flag:
                self.condition.wait()
            else:
                if self._balance >= draw_amount:
                    self._balance = self._balance - draw_amount
                    print(threading.current_thread().getName() + " 取錢成功,賬戶余額是:" + str(self._balance) + "\n")
                else:
                    print(threading.current_thread().getName() + " 取錢失敗\n")
                # 將標(biāo)識賬戶已有存款的標(biāo)識改成False
                self.__deposit_flag = False
                # 喚醒其他等待現(xiàn)車線程
                self.condition.notify_all()
        finally:
            # 釋放鎖
            self.condition.release()
 
    # 存錢
    def deposit(self, deposit_amount):
        # 加鎖
        self.condition.acquire()
        try:
            # 如果已經(jīng)存款了,則等待取款
            if self.__deposit_flag:
                self.condition.wait()
            else:
                self._balance = self._balance + deposit_amount
                print(threading.current_thread().getName() + " 存款成功,存款金額是:" + str(deposit_amount) + "\n")
                # 將存款標(biāo)識改成已存款
                self.__deposit_flag = True
                # 喚醒其他線程
                self.condition.notify_all()
        finally:
            # 釋放鎖
            self.condition.release()
 
 
def draw_many(account, draw_amount, max):
    for i in range(max):
        account.draw(draw_amount)
 
 
def deposit_many(account, deposit_amount, max):
    for i in range(max):
        account.deposit(deposit_amount)
 
 
# 創(chuàng)建一個賬戶
account = Account("賬戶一", 0)
# 創(chuàng)建并啟動取錢線程
draw_1 = threading.Thread(name='取錢者一', target=draw_many, args=(account, 200, 50))
draw_1.start()
draw_2 = threading.Thread(name='取錢者二', target=draw_many, args=(account, 200, 50))
draw_2.start()
# 創(chuàng)建并啟動存錢線程
deposit_1 = threading.Thread(name='存錢者一', target=deposit_many, args=(account, 200, 50))
deposit_1.start()
deposit_2 = threading.Thread(name='存錢者二', target=deposit_many, args=(account, 200, 50))
deposit_2.start()
draw_1.join()
draw_2.join()
deposit_1.join()
deposit_2.join()

以上就是python線程通信Condition提供的方法,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。


當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Python教程:python線程通信Condition提供的方法
瀏覽路徑:http://m.5511xx.com/article/cojdjeo.html