'''
Created on Jun 17, 2013 @author: smp
'''
#-*- coding:utf-8 -*- import threading
import time counter = 0
mutex=threading.Lock() class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self) def run(self):
global counter,mutex
time.sleep(1);
if mutex.acquire():
counter += 1
print "I am %s,set counter:%s" %(self.name,counter)
mutex.release() if __name__=="__main__":
for i in range(0,10):
my_thread = MyThread()
my_thread.start()
I am Thread-1,set counter:1
I am Thread-2,set counter:2
I am Thread-4,set counter:3
I am Thread-3,set counter:4
I am Thread-5,set counter:5
I am Thread-6,set counter:6
I am Thread-7,set counter:7
I am Thread-9,set counter:8
I am Thread-10,set counter:9
I am Thread-8,set counter:10

python 多线程一(lock)的更多相关文章

  1. python多线程threading.Lock锁用法实例

    本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...

  2. python多线程锁lock/Rlock/BoundedSemaphore/Condition/Event

    import time import threading lock = threading.RLock() n = 10 def task(arg): # 加锁,此区域的代码同一时刻只能有一个线程执行 ...

  3. python 多线程中的同步锁 Lock Rlock Semaphore Event Conditio

    摘要:在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lo ...

  4. python多线程学习记录

    1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...

  5. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  6. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  7. 【python,threading】python多线程

    使用多线程的方式 1.  函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...

  8. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

  9. Python多线程和Python的锁

    Python多线程 Python中实现多线程有两种方式,一种基于_thread模块(在Python2.x版本中为thread模块,没有下划线)的start_new_thread()函数,另一种基于th ...

  10. 【跟我一起学Python吧】Python 多线程

    其实自我感觉Python的多线程很类似于Java的多线程机制,但是比JAVA的多线程更灵活.在早期的Python多线程实现中,采用了thread模块.例如: from time import ctim ...

随机推荐

  1. 解析stm32的时钟

    STM32 时钟系统  http://blog.chinaunix.net/uid-24219701-id-4081961.html STM32的时钟系统 ***   http://www.cnblo ...

  2. 简单字符串处理 hdu2532 Engine

    本来可以把这篇文章放入上一篇文章里,不过做这个题花了一点时间,也有一点收获,同时觉得网上的这个题目可供参考的文章有些少,那么就单独成篇吧. 首先分析下题目思路: 这个题目是个模拟题,步骤也很清晰. 首 ...

  3. docker学习笔记3:镜像操作(查找和下载)

    一.查看本地镜像 只有下载后,镜像才会保存在本地(docker环境所在的主机),通过如下命令可以查看本地已经存在的镜像. 命令:dokcer images 上面命令列出本地所有已经存在的镜像,显示的信 ...

  4. img src某个php文件输出图片(回复更改图片readfile读取图片等)

    在论坛我们经常看到一回复图片就更改等,这功能是怎么实现的呢,其实更验证码道理相同. 新建文件 randimage.php 加入以下代码: <?php $dir='../../images/'; ...

  5. cmake总结

    无论生活还是工作上,做过的事,需要总结下. 接触cmake,一般都会看一本书 <<cmake 实践>>. 这是cmake的入门书.我就不多说了. 下面说一下我对部分cmake命 ...

  6. Java I/O流-ObjectInputStream、ObjectOutputStream

    一.整体代码 ObjectStreamDemo.java import java.io.File; import java.io.FileInputStream; import java.io.Fil ...

  7. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3

    点击下载代码   http://download.csdn.net/detail/lideguo1979/8291803 新建一个类RouteNavigation,定义getPath()方法.用来获取 ...

  8. linux c setitimer使用方法说明

    在linux c编程中.setitimer是一个比較经常使用的函数.可用来实现延时和定时的功能,网上有各种零零散散的使用方法说明,都仅仅提到了个别使用方法,今天抽出时间实践整理了一份比較具体的: 使用 ...

  9. android中获取 bitmap 像素的颜色 之吸管取色功能

    本功能是参考android API colorPickerView修改,实现类似与PS中吸管取色功能.也就是可以对图片的任意位置取该位置的RGB.本demo中,完成了色盘取色功能.当点击色盘的某个位置 ...

  10. OCP-1Z0-051-题目解析-第8题

    8. View the Exhibit and examine the structure of the CUSTOMERS table. Which two tasks would require ...