码迷,mamicode.com
首页 > 编程语言 > 详细

python-线程三(锁)

时间:2020-03-28 20:14:36      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:must   ini   设置   目的   获得   一个   reading   thread   get   

锁:可以保证线程同步和线程安全

  1)Lock锁

方式1:Lock
from threading import Thread, Lock
import threading


def run(lock, num): 
    lock.acquire()  # 获得锁
    threadName = threading.current_thread().getName()  # 取得当前线程的线程名
    print("%s, Hello Num: %s" %(threadName, num))
    lock.release()  # 释放锁


if __name__ == __main__: 
    lock = Lock()  # 创建一个共享锁实例
    for num in range(20):
        Thread(name=Thread-%s % str(num), target=run, args=(lock, num)).start()


# 方式2
import threading
import time
data = 0
lock = threading.Lock()  # 创建一个锁对象


def func():
    global data
    print("%s acquire lock...\n" % threading.currentThread().getName())
    if lock.acquire():
        print("%s get lock...\n" % threading.currentThread().getName())
        data += 1  # must lock
        time.sleep(2)  # 其它操作
        print("%s release lock...\n" % threading.currentThread().getName())  # 调用release()将释放锁
        lock.release()


startTime = time.time()
t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)
t3 = threading.Thread(target=func)
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
endTime = time.time()
print("used time is", endTime - startTime)

  2)Semaphore锁

from threading import Thread, Lock
import threading
import time


def worker(s, i):
    s.acquire()
    print(threading.current_thread().name + " acquire")
    time.sleep(i)
    print(threading.current_thread().name + " release")
    s.release()

    
if __name__ == "__main__":
    # 设置限制最多3个线程同时访问共享资源
    s = threading.Semaphore(3)
    for i in range(5):
        t = Thread(target=worker, args=(s, i * 2))
        t.start()

 

死锁

import threading 
import time
lock1 = threading.Lock() 
lock2 = threading.Lock() 
print(lock1, lock2)


class T1(threading.Thread): 
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.t_name = name
        
    def run(self):
        lock1.acquire()
        time.sleep(1)   # 睡眠的目的是让线程2获得调度,得到第二把锁
        print(in thread T1,self.t_name)
        time.sleep(2)
        lock2.acquire()  # 线程1请求第二把锁
        print(in lock l2 of T1)
        lock2.release()
        lock1.release()


class T2(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.t_name = name
        
    def run(self):
        lock2.acquire()
        time.sleep(2)  # 睡眠的目的是让线程1获得调度,得到第一把锁
        print(in thread T2,self.t_name)
        lock1.acquire()  # 线程2请求第一把锁
        print(in lock l1 of T2)
        lock1.release()
        lock2.release()
        
        
def test():
    thread1 = T1(A)
    thread2 = T2(B)
    thread1.start()
    thread2.start()
    
    
if __name__== __main__:
    test()

# 伪代码:

死锁:
锁1
锁2


线程1:
锁1.acquire() # 获得锁1
sleep(2)
锁2.acquire() # 获取锁2 的时候要等线程2释放了才可以获取
锁1.release() # 要等上一步执行了才可以释放锁1


线程2:
锁2.acquire() # 获得锁2
sleep(2)
锁1.acquire() # 获取锁1 的时候要等线程1释放了才可以获取
锁2.release() 要等上一步执行了才可以释放锁2

 

python-线程三(锁)

标签:must   ini   设置   目的   获得   一个   reading   thread   get   

原文地址:https://www.cnblogs.com/su-sir/p/12588894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!