start_new_thread(function,atgs[,kwargs])
>>> import thread
>>> def run(n):
for i in range(n):
print i
>>> thread.start_new_thread(run,(4,)) #注意第二个参数一定要是元组的形式
53840
1
>>>
2
3
KeyboardInterrupt
>>> thread.start_new_thread(run,(2,))
17840
1
>>>
thread.start_new_thread(run,(),{'n':4})
39720
1
>>>
2
3
thread.start_new_thread(run,(),{'n':3})
32480
1
>>>
2
>>> import threading
>>> class mythread(threading.Thread):
def __init__(self,num):
threading.Thread.__init__(self)
self.num = num
def run(self): #重载run方法
print 'I am', self.num
>>> t1 = mythread(1)
>>> t2 = mythread(2)
>>> t3 = mythread(3)
>>> t1.start() #运行线程t1
I am
>>> 1
t2.start()
I am
>>> 2
t3.start()
I am
>>> 3
import threading
>>> def run(x,y):
for i in range(x,y):
print i
>>> t1 = threading.Thread(target=run,args=(15,20)) #直接使用Thread附加函数args为函数参数
>>> t1.start()
15
>>>
16
17
18
19
>>> import threading
>>> import time
>>> class mythread(threading.Thread):
def __init__(self,id):
threading.Thread.__init__(self)
self.id = id
def run(self):
time.sleep(5) #休眠5秒
print self.id
>>> t = mythread(1)
>>> def func():
t.start()
print t.isAlive() #打印线程状态
>>> func()
True
>>> 1
import threading
>>> import time #导入time模块
>>> class Mythread(threading.Thread):
def __init__(self,id):
threading.Thread.__init__(self)
self.id = id
def run(self):
x = 0
time.sleep(20)
print self.id
>>> def func():
t.start()
for i in range(5):
print i
>>> t = Mythread(2)
>>> func()
0
1
2
3
4
>>> 2
def func():
t.start()
t.join()
for i in range(5):
print i
>>> t = Mythread(3)
>>> func()
3
0
1
2
3
4
>>>
>>> import threading
>>> class mythread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name=threadname)
def run(self):
print self.getName()
>>>
>>> t1 = mythread('t1')
>>> t1.start()
t1
>>>
# -*- coding:utf-8 -*-
import threading
import time
class mythread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global x #设置全局变量
# lock.acquire() #调用lock的acquire方法
for i in range(3):
x = x + 1
time.sleep(2)
print x
# lock.release() #调用lock的release方法
#lock = threading.RLock() #生成Rlock对象
t1 = []
for i in range(10):
t = mythread(str(i))
t1.append(t)
x = 0 #将全局变量的值设为0
for i in t1:
i.start()
E:/study/<a href="http://lib.csdn.net/base/python" rel="external nofollow" class='replace_word' title="Python知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Python</a>/workspace>xianchengtongbu.py
3
6
9
12
15
18
21
24
27
30
# -*- coding:utf-8 -*-
import threading
class Producer(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global x
con.acquire()
if x == 1000000:
con.wait()
# pass
else:
for i in range(1000000):
x = x + 1
con.notify()
print x
con.release()
class Consumer(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global x
con.acquire()
if x == 0:
con.wait()
#pass
else:
for i in range(1000000):
x = x - 1
con.notify()
print x
con.release()
con = threading.Condition()
x = 0
p = Producer('Producer')
c = Consumer('Consumer')
p.start()
c.start()
p.join()
c.join()
print x
E:/study/python/workspace>xianchengtongbu2.py
1000000
0
0
# -*- coding:utf-8 -*-
import threading
class mythread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global event
if event.isSet():
event.clear()
event.wait() #当event被标记时才返回
print self.getName()
else:
print self.getName()
event.set()
event = threading.Event()
event.set()
t1 = []
for i in range(10):
t = mythread(str(i))
t1.append(t)
for i in t1:
i.start()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有