Day20 作业

使用锁

"""______lxh______"""
from threading import Thread, Lock
from time import sleep

list1 = [1, 2, 3]
lock = Lock()


def func1():
    lock.acquire()
    global list1
    list2 = list1[:]
    sleep(3)
    list2.append(100)
    list1 = list2
    lock.release()


def func2():
    lock.acquire()
    global list1
    list2 = list1[:]
    sleep(3)
    list2.remove(2)
    list1 = list2
    lock.release()


t1 = Thread(target=func1)
t2 = Thread(target=func2)
t1.start()
t2.start()
t1.join()
t2.join()
print(list1)

你可能感兴趣的:(Day20 作业)