---
# System prepended metadata

title: ch30_27.py

---

# ch30_27.py
```
# https://medium.com/jeasee隨筆/python-多線程-eb36272e604b
import threading, time
semaphore = threading.BoundedSemaphore(3) # 限制計數器最大值
 
def func():
    if semaphore.acquire(): # 如果取得鎖
        print(f'{threading.current_thread().name} 取得鎖')
        print("Working ...")
        #time.sleep(2)
        semaphore.release()
        print(f'{threading.current_thread().name} 釋出鎖')
  
for i in range(5):
    t = threading.Thread(target=func)
    t.start()
```