线程同步

//test.py

import threading

import time

exitFlag = 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 "Starting " + self.name

threadLock.acquire()

print_time(self.name, self.counter, 5)

threadLock.release()

print "Exiting " + self.name

def print_time(threadName, delay, counter):

while counter:

if exitFlag:

#threading.Thread.exit()

return

time.sleep(delay)

print "%s: %s" % (threadName, time.ctime(time.time()))

counter -= 1

threadLock = threading.Lock()

threads = []

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread(2, "Thread-2", 2)

thread1.start()

thread2.start()

#exitFlag = 1

threads.append(thread1)

threads.append(thread2)

for t in threads:

t.join()

print "Exiting Main Thread"

//result

# python test.py
Starting Thread-1
Starting Thread-2
Thread-2: Wed Nov 15 18:31:53 2017
Thread-2: Wed Nov 15 18:31:55 2017
Thread-2: Wed Nov 15 18:31:57 2017
Thread-2: Wed Nov 15 18:31:59 2017
Thread-2: Wed Nov 15 18:32:01 2017
Exiting Thread-2
Thread-1: Wed Nov 15 18:32:02 2017
Thread-1: Wed Nov 15 18:32:03 2017
Thread-1: Wed Nov 15 18:32:04 2017
Thread-1: Wed Nov 15 18:32:05 2017
Thread-1: Wed Nov 15 18:32:06 2017
Exiting Thread-1
Exiting Main Thread

Finally:

看到没有,关键任务是大家排排队,吃果果。

不要争,不要抢,谁先干的,就让他先干完,咋这么过瘾呢!!!:):)

python threading acquire release的更多相关文章

  1. Python学习笔记- Python threading模块

    Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...

  2. Python threading(多线程)

    threading模块在较低级别thread模块之上构建更高级别的线程接口. 一.threading模块定义了以下函数和对象: threading.active_count() 等同于threadin ...

  3. python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)

    今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...

  4. python threading.thread

    Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...

  5. python threading模块的Lock和RLock区别

    首先了解这两者是什么. 以下说明参考自python官网 Lock:Lock被称为①原始锁,原始锁是一个②在锁定时不属于特定线程的同步基元组件,它是能用的最低级的同步基元组件.原始锁处于 "锁 ...

  6. [Python]threading local 线程局部变量小測试

    概念 有个概念叫做线程局部变量.一般我们对多线程中的全局变量都会加锁处理,这样的变量是共享变量,每一个线程都能够读写变量,为了保持同步我们会做枷锁处理.可是有些变量初始化以后.我们仅仅想让他们在每一个 ...

  7. python——threading模块

    一.什么是线程 线程是操作系统能够进行运算调度的最小单位.进程被包含在进程中,是进程中实际处理单位.一条线程就是一堆指令集合. 一条线程是指进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条 ...

  8. python threading编程中的LOCK和RLOCK(可重入锁)

    找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...

  9. python threading基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' """ python是支持多线程的,并 ...

随机推荐

  1. IBatisNet不常用到的配置(Dao.config ConnectionTimeout),居然不起作用(前辈留给我们的坑)

    IBattis 默认超时时间好像是30s,可多于这个时间总就会报错:DaoProxy : unable to intercept method name 'ExcuteQuery', cause : ...

  2. Python学习笔记之--我又开始学习Python了(随时更新)

    2019.02.09 更新 Python 学习计划已经开始几天了,跟着一本叫<Django for beginner>的书籍在学习用Django搭建自己的第一个网站,目前已经进行到第三章, ...

  3. Codeforces 670E - Correct Bracket Sequence Editor - [链表]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  4. Python:time模块、calendar模块

    time模块 import time 获取时间戳 >>>time.time() #1532418950.7246091 获取时间元组 >>> time.localt ...

  5. vue中的provide/inject的学习

    在 Vue.js 的 2.2.0+ 版本中添加加了 provide 和 inject 选项.用于父级组件向下传递数据.provide/inject:简单的来说就是在父组件(或者曾祖父组件)中通过pro ...

  6. [cloud][sdn] network namespace

    man 手册关于IP netns的介绍: http://man7.org/linux/man-pages/man8/ip-netns.8.html 一个非常好的介绍,有概念,有操作: http://c ...

  7. [https][openssl] OpenSSL 公钥、私钥以及自签名证书

    转自:https://www.zybuluo.com/muyanfeixiang/note/392079 简介 公钥私钥用来互相加解密的一对密钥,一般是采用RSA非对称算法.公钥加密的私钥能解密,私钥 ...

  8. airflow 实战

    def print_hello(*a,**b): print a print "=========" print b print 'Hello world!' raise Valu ...

  9. Orchard Core 模块化

    在上一篇文章谈到如何搭好一个基础的Orchard Core项目. 今天要尝试Orchard Core的模块化. 我自己的理解:一个系统可以分成一个个模块,这一个个模块是由一个个类库去实现的. 首先,在 ...

  10. 【Python基础】json.dumps()和json.loads()、json.dump()和json.load()的区分

    json文件处理涉及的四个函数json.dumps()和json.loads().json.dump()和json.load()的区分 一.概念理解 1.json.dumps()和json.loads ...