代码如下
python
#!/usr/bin/env python # -*- coding: utf-8 -*- import socket,time,threading task_num=0 lock = threading.Lock() def loop4thread(): global task_num while task_num < 100: lock.acquire() try: print "%s Doing NO.%d Task..." %(threading.current_thread().name , task_num) task_num+=1 finally: lock.release() onethread=[ 0 for x in range(10)] for i in range(10) : onethread[i] = threading.Thread(target=loop4thread,name="Threadloop"+str(i)) onethread[i].start()
输出结果 最后一行是Doing No.108 Task...
可是我在loop4thread函数中写了 在 task_num <100
才执行啊,
为什么 task_num 溢出到 108 呢
慕尼黑的夜晚无繁华
相关分类