zhoushixiong

Python 线程同步

以下代码可以直观展示加锁和不加锁时,对数据修改情况。

加锁时

# -*-* encoding:UTF-8 -*-
# author : shoushixiong
# date   : 2018/11/22
import threading
import time
list = [0,0,0,0,0,0,0,0,0,0,0,0]
class myThread(threading.Thread):
    def __init__(self,threadId,name,counter):
        threading.Thread.__init__(self)
        self.threadId = threadId
        self.name = name
        self.counter = counter
    def run(self):
        print "开始线程:",self.name
        # 获得锁,成功获得锁定后返回 True
        # 可选的timeout参数不填时将一直阻塞直到获得锁定
        # 否则超时后将返回 False
        threadLock.acquire()
        print_time(self.name,self.counter,list.__len__())
        # 释放锁
        threadLock.release()
    def __del__(self):
        print self.name,"线程结束!"
def print_time(threadName,delay,counter):
    while counter:
        time.sleep(delay)
        list[counter-1] += 1
        print "[%s] %s 修改第 %d 个值,修改后值为:%d" % (time.ctime(time.time()),threadName,counter,list[counter-1])
        counter -= 1
threadLock = threading.Lock()
threads = []
# 创建新线程
thread1 = myThread(1,"Thread-1",1)
thread2 = myThread(2,"Thread-2",2)
# 开启新线程
thread1.start()
thread2.start()
# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有线程完成
for t in threads:
    t.join()
print "主进程结束!"

输出结果为:

开始线程: Thread-1
开始线程: Thread-2
[Thu Nov 22 16:04:13 2018] Thread-1 修改第 12 个值,修改后值为:1
[Thu Nov 22 16:04:14 2018] Thread-1 修改第 11 个值,修改后值为:1
[Thu Nov 22 16:04:15 2018] Thread-1 修改第 10 个值,修改后值为:1
[Thu Nov 22 16:04:16 2018] Thread-1 修改第 9 个值,修改后值为:1
[Thu Nov 22 16:04:17 2018] Thread-1 修改第 8 个值,修改后值为:1
[Thu Nov 22 16:04:18 2018] Thread-1 修改第 7 个值,修改后值为:1
[Thu Nov 22 16:04:19 2018] Thread-1 修改第 6 个值,修改后值为:1
[Thu Nov 22 16:04:20 2018] Thread-1 修改第 5 个值,修改后值为:1
[Thu Nov 22 16:04:21 2018] Thread-1 修改第 4 个值,修改后值为:1
[Thu Nov 22 16:04:22 2018] Thread-1 修改第 3 个值,修改后值为:1
[Thu Nov 22 16:04:23 2018] Thread-1 修改第 2 个值,修改后值为:1
[Thu Nov 22 16:04:24 2018] Thread-1 修改第 1 个值,修改后值为:1
[Thu Nov 22 16:04:26 2018] Thread-2 修改第 12 个值,修改后值为:2
[Thu Nov 22 16:04:28 2018] Thread-2 修改第 11 个值,修改后值为:2
[Thu Nov 22 16:04:30 2018] Thread-2 修改第 10 个值,修改后值为:2
[Thu Nov 22 16:04:32 2018] Thread-2 修改第 9 个值,修改后值为:2
[Thu Nov 22 16:04:34 2018] Thread-2 修改第 8 个值,修改后值为:2
[Thu Nov 22 16:04:36 2018] Thread-2 修改第 7 个值,修改后值为:2
[Thu Nov 22 16:04:38 2018] Thread-2 修改第 6 个值,修改后值为:2
[Thu Nov 22 16:04:40 2018] Thread-2 修改第 5 个值,修改后值为:2
[Thu Nov 22 16:04:42 2018] Thread-2 修改第 4 个值,修改后值为:2
[Thu Nov 22 16:04:44 2018] Thread-2 修改第 3 个值,修改后值为:2
[Thu Nov 22 16:04:46 2018] Thread-2 修改第 2 个值,修改后值为:2
[Thu Nov 22 16:04:48 2018] Thread-2 修改第 1 个值,修改后值为:2
主进程结束!
Thread-1 线程结束!
Thread-2 线程结束!

不加锁时

同样是上面实例的代码,注释以下两行代码:
threadLock.acquire()
threadLock.release()

输出结果为:

开始线程: Thread-1
开始线程: Thread-2
[Thu Nov 22 16:09:20 2018] Thread-1 修改第 12 个值,修改后值为:1
[Thu Nov 22 16:09:21 2018] Thread-2 修改第 12 个值,修改后值为:2
[Thu Nov 22 16:09:21 2018] Thread-1 修改第 11 个值,修改后值为:1
[Thu Nov 22 16:09:22 2018] Thread-1 修改第 10 个值,修改后值为:1
[Thu Nov 22 16:09:23 2018] Thread-1 修改第 9 个值,修改后值为:1
[Thu Nov 22 16:09:23 2018] Thread-2 修改第 11 个值,修改后值为:2
[Thu Nov 22 16:09:24 2018] Thread-1 修改第 8 个值,修改后值为:1
[Thu Nov 22 16:09:25 2018] Thread-2 修改第 10 个值,修改后值为:2
[Thu Nov 22 16:09:25 2018] Thread-1 修改第 7 个值,修改后值为:1
[Thu Nov 22 16:09:26 2018] Thread-1 修改第 6 个值,修改后值为:1
[Thu Nov 22 16:09:27 2018] Thread-2 修改第 9 个值,修改后值为:2
[Thu Nov 22 16:09:27 2018] Thread-1 修改第 5 个值,修改后值为:1
[Thu Nov 22 16:09:28 2018] Thread-1 修改第 4 个值,修改后值为:1
[Thu Nov 22 16:09:29 2018] Thread-2 修改第 8 个值,修改后值为:2
[Thu Nov 22 16:09:29 2018] Thread-1 修改第 3 个值,修改后值为:1
[Thu Nov 22 16:09:30 2018] Thread-1 修改第 2 个值,修改后值为:1
[Thu Nov 22 16:09:31 2018] Thread-2 修改第 7 个值,修改后值为:2
[Thu Nov 22 16:09:31 2018] Thread-1 修改第 1 个值,修改后值为:1
[Thu Nov 22 16:09:33 2018] Thread-2 修改第 6 个值,修改后值为:2
[Thu Nov 22 16:09:35 2018] Thread-2 修改第 5 个值,修改后值为:2
[Thu Nov 22 16:09:37 2018] Thread-2 修改第 4 个值,修改后值为:2
[Thu Nov 22 16:09:39 2018] Thread-2 修改第 3 个值,修改后值为:2
[Thu Nov 22 16:09:41 2018] Thread-2 修改第 2 个值,修改后值为:2
[Thu Nov 22 16:09:43 2018] Thread-2 修改第 1 个值,修改后值为:2
主进程结束!
Thread-1 线程结束!
Thread-2 线程结束!