标签:
thread/threading
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import thread
import 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, random
count = 0
lock = thread.allocate_lock() #创建一个琐对象
def threadTest():
global count, lock
print lock.acquire() #获取琐
for i in xrange(10000):
count += 1
print 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