RLock to multi-thread synchronization

sponsored links
Problem scenario:
There are multiple threads, are responsible for the construction of houses, trees, gardening and the like work, which requires a certain waiting time, waiting for the process need to communicate with the server to stay connected. Submission can not be too frequent connection, preferably 2-3 seconds again.
For this synchronization between multiple threads can use RLock to achieve lock mechanism.
#coding=utf-8
'''
Created on 2010-9-14
@author: stonelee
'''
import time
import random
import threading
p_lock = threading.RLock()
s_lock = threading.RLock()

def my_post(str):
    p_lock.acquire()
    print str
    time.sleep(random.random() * 2)
    p_lock.release()
        
def my_sleep(seconds):   
    s_lock.acquire()
    time.sleep(seconds)
    s_lock.release()    
     
def my_wait(name, seconds):
    last_time = time.time()
    last_seconds = 0
    while last_seconds < seconds:
        my_post('%s:%s' % (time.time(), name)) 
        my_sleep(3)
        last_seconds = time.time() - last_time
    
class ThreadA(threading.Thread):
    def run(self):
        for i in range(10000):
            my_post('%s:==================build house' % time.time())
            my_wait('a wait', 20)
            
class ThreadB(threading.Thread):
    def run(self):
        for i in range(10000):
            my_post('%s:=================plant tree' % time.time())
            my_wait('b wait', 5)
            
threadA = ThreadA()
threadA.setDaemon(True)
threadB = ThreadB()
threadB.setDaemon(True)

threadA.start()
threadB.start()
threadA.join()
threadB.join()
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of RLock to multi-thread synchronization

  • hibernate call stored procedure

    hibernate call stored procedure

  • Hibernate pessimistic locking mechanism for locking and optimistic locking

    hibernate lock mechanism 1. Pessimistic lock It refers to the modification of data by outsiders hold a conservative attitude. The assumption that at any time access to data, may also have another client to access the same data, in order to maintain t ...

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries