print "=======================thread.start_new_thread启动线程============="
import thread
#Python的线程sleep方法并不是在thread模块中,反而是在time模块下
import time
def inthread(no,interval):
count=0
while count<10:
print "Thread-%d,休眠间隔:%d,current Time:%s"%(no,interval,time.ctime())
#使当前线程休眠指定时间,interval为浮点型的秒数,不同于Java中的整形毫秒数
time.sleep(interval)
#Python不像大多数高级语言一样支持++操作符,只能用+=实现
count+=1
else:
print "Thread-%d is over"%no
#可以等待线程被PVM回收,或主动调用exit或exit_thread方法结束线程
thread.exit_thread()
#使用start_new_thread函数可以简单的启动一个线程,第一个参数指定线程中执行的函数,第二个参数为元组型的传递给指定函数的参数值
thread.start_new_thread(inthread,(1,2))
#线程执行时必须添加这一行,并且sleep的时间必须足够使线程结束,如本例
#如果休眠时间改为20,将可能会抛出异常
time.sleep(30)
'''
Unhandled exception in thread started by Error in sys.excepthook: Original exception was:
import thread;
from time import sleep,ctime;
from random import choice
#The first param means the thread number
#The second param means how long it sleep
#The third param means the Lock
def loop(nloop,sec,lock):
print "Thread ",nloop," start and will sleep ",sec;
sleep(sec);
print "Thread ",nloop," end ",sec;
lock.release();
def main():
seconds=[4,2];
locks=[];
for i in range(len(seconds)) :
lock=thread.allocate_lock();
lock.acquire();
locks.append(lock);
print "main Thread begins:",ctime();
for i,lock in enumerate(locks):
thread.start_new_thread(loop,(i,choice(seconds),lock));
for lock in locks :
while lock.locked() :
pass;
print "main Thread ends:",ctime();
if __name__=="__main__" :
main();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有