通过RLOCK方式,解决多线程之间修改共享对象不出问题,使线程的资源独占
import threading import time lock=threading.Rlock() def run(name): lock.acquire() print "i am mult-threding",i lock.release() time.sleep(2) for i in range(20): t=threading.Thread(target=run,args=(i,)) t.start()
通过RLOCK方式,解决多线程之间修改共享对象不出问题,使线程的资源独占
import threading import time lock=threading.Rlock() def run(name): lock.acquire() print "i am mult-threding",i lock.release() time.sleep(2) for i in range(20): t=threading.Thread(target=run,args=(i,)) t.start()
上一篇:生产者消费者模型
13.8 线程加锁的设置,所谓线程就是为了防止多个线程为同一个动作进行操作。synchronized是Java中的关键字,是一种同步锁。它修饰的对象有以下几种法的对象
线程的使用 1. 导入包 import threading 2.创建线程 thread1 = threading.Thread(tag="函数名",args=(参数,)) 3.开启线程 thread1.st
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M