您的当前位置:首页学习Python5(Thread)
学习Python5(Thread)
来源:锐游网
测试代码
#coding:utf8
import thread, time, random
print '-'*25, '测试1', '-'*25
count = 0
lock = thread.allocate_lock()
def threadTest():
global count, lock
lock.acquire()
for i in xrange(1000000):
count += 1
lock.release()
for i in range(5):
thread.start_new_thread(threadTest, ()) #如果对start_new_thread函数不是很了解,不要着急,马上就会讲解
# print thread.get_ident()
# print thread.stack_size()
time.sleep(0.1)
print count #count是多少呢?是10000 * 10 吗?
# print '-'*25, '测试2', '-'*25
# def threadFunc(a = None, b = None, c = None, d = None):
# print time.strftime('%H:%M:%S', time.localtime()), a
# time.sleep(1)
# print time.strftime('%H:%M:%S', time.localtime()), b
# time.sleep(1)
# print time.strftime('%H:%M:%S', time.localtime()), c
# time.sleep(1)
# print time.strftime('%H:%M:%S', time.localtime()), d
# time.sleep(1)
# print time.strftime('%H:%M:%S', time.localtime()), 'over'
# thread.start_new_thread(threadFunc, (3, 4, 5, 6)) #创建线程,并执行threadFunc函数。
# time.sleep(5)
# print '-'*25, '测试3:', '-'*25
# thread.start_new_thread(lambda : (thread.interrupt_main(), ), ())
# try:
# time.sleep(2)
# except KeyboardInterrupt, e:
# print 'error:', e
# print 'over'
测试结果
------------------------- 测试1 -------------------------
5000000
>>>
因篇幅问题不能全部显示,请点此查看更多更全内容