日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中locked_Python锁类| 带示例的locked()方法

發布時間:2025/3/11 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中locked_Python锁类| 带示例的locked()方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python中locked

Python Lock.locked()方法 (Python Lock.locked() Method)

locked() is an inbuilt method of the Lock class of the threading module in Python.

Locked()是Python中線程模塊的Lock類的內置方法。

This method returns True if the lock is acquired by a thread else returns False.

如果線程獲取了鎖,則此方法返回True ,否則返回False 。

Module:

模塊:

from threading import Lock

Syntax:

句法:

locked()

Parameter(s):

參數:

  • None

    沒有

Return value:

返回值:

The return type of this method is <class 'bool'>. It returns True is the lock is currently acquired else returns False.

此方法的返回類型為<class'bool'> 。 返回True是當前獲取的鎖,否則返回False 。

Example:

例:

# Python program to show # the use of locked() method in Lock classimport threading import randomclass shared(object):def __init__(self, x = 0):# Created a Lock objectself.lock = threading.Lock()self.incr = x# Increment function for the threaddef incrementcounter(self):print("Waiting for the lock to be unlocked")# Lock acquired by the current threadself.lock.acquire()print("Is the lock acquired here:", self.lock.locked())try:print('Lock acquired, current counter value: ', self.incr)self.incr = self.incr + 1print("Is the lock acquired here as well:", self.lock.locked())finally:print('Lock released, current counter value: ', self.incr)# Lock released by the given threadself.lock.release()print("Is the lock acquired here after release:", self.lock.locked())def helper_thread(c):# Getting a random integer between 1 to 3r = random.randint(1,3)print("Random value selected:", r)for i in range(r):c.incrementcounter()print('Finished', str(threading.current_thread().getName()))print()if __name__ == '__main__':obj = shared()thread1 = threading.Thread(target=helper_thread, args=(obj,))thread1.start()thread2 = threading.Thread(target=helper_thread, args=(obj,))thread2.start()thread1.join()thread2.join()print('Final counter value:', obj.incr)

Output

輸出量

Waiting for the lock to be unlocked Is the lock acquired here: True Lock acquired, current counter value: 0 Is the lock acquired here as well: True Lock released, current counter value: 1 Is the lock acquired here after release: False Finished Thread-1Random value selected: 3 Waiting for the lock to be unlocked Is the lock acquired here: True Lock acquired, current counter value: 1 Is the lock acquired here as well: True Lock released, current counter value: 2 Is the lock acquired here after release: False Waiting for the lock to be unlocked Is the lock acquired here: True Lock acquired, current counter value: 2 Is the lock acquired here as well: True Lock released, current counter value: 3 Is the lock acquired here after release: False Waiting for the lock to be unlocked Is the lock acquired here: True Lock acquired, current counter value: 3 Is the lock acquired here as well: True Lock released, current counter value: 4 Is the lock acquired here after release: False Finished Thread-2Final counter value: 4

翻譯自: https://www.includehelp.com/python/lock-locked-method-with-example.aspx

python中locked

總結

以上是生活随笔為你收集整理的python中locked_Python锁类| 带示例的locked()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。