标签:
thread/threading
#!/usr/bin/python# -*- coding: UTF-8 -*-import threadimport time# 为线程定义一个函数def threadTest(threadName):print "%s" %(threadName)# 创建两个线程try:thread.start_new_thread( threadTest, ("Thread-1",) )time.sleep(0.3)thread.start_new_thread( threadTest, ("Thread-2",) )time.sleep(0.3)except:print "Error: unable to start thread"
#!/usr/bin/python# -*- coding: UTF-8 -*-import thread, time, randomcount = 0lock = thread.allocate_lock() #创建一个琐对象def threadTest():global count, lockprint lock.acquire() #获取琐for i in xrange(10000):count += 1print lock.release() #释放琐#print thread.exit ()for i in xrange(10):thread.start_new_thread(threadTest, ())time.sleep(0.3)print count
标签:
原文地址:http://www.cnblogs.com/lshconfigure/p/5775262.html