pythonx学习2019-2-26

import threading,time
def run():
    for i in range(100):
        time.sleep(0.1)
    print("子线程结束")
t1 = threading.Thread(target=run)
t1.setDaemon(True)#False
t1.start()
t1.join(timeout=5)
print("主线程结束")

'''
'''
import threading,time
rlock1 = threading.RLock()
rlock2 = threading.RLock()
rlock3 = threading.RLock()
rlock4 = threading.RLock()
rlock5 = threading.RLock()

class ZheXueJia():
    def __init__(self,left,right):
        self.left = left
        self.right = right

z1 = ZheXueJia(rlock5,rlock1)
z2 = ZheXueJia(rlock1,rlock2)
z3 = ZheXueJia(rlock2,rlock3)
z4 = ZheXueJia(rlock3,rlock4)
z5 = ZheXueJia(rlock4,rlock5)

def run(z,name):

    f = z.left.acquire()
    if f:
        print(name,"获取左筷子")
        ff = z.right.acquire()
        if ff:
            print(name, "获取右筷子")
            print("哲学家开始就餐",name)
            time.sleep(1)
    z.right.release()
    z.left.release()


t1 = threading.Thread(target=run,args=(z1,"z1"))
t2 = threading.Thread(target=run,args=(z2,"z2"))
t3 = threading.Thread(target=run,args=(z3,"z3"))
t4 = threading.Thread(target=run,args=(z4,"z4"))
t5 = threading.Thread(target=run,args=(z5,"z5"))

t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
'''
'''
#信号量
import threading,time
s = threading.Semaphore(3)
def run(name):
    s.acquire()
    print(name,"开始执行")
    time.sleep(1)
    print(name,"执行结束")
    s.release()
start_time = time.time()
t1 = threading.Thread(target=run,args=("t1",))
t2 = threading.Thread(target=run,args=("t2",))
t3 = threading.Thread(target=run,args=("t3",))
t4 = threading.Thread(target=run,args=("t4",))
t5 = threading.Thread(target=run,args=("t5",))
t6 = threading.Thread(target=run,args=("t6",))
t7 = threading.Thread(target=run,args=("t7",))
t8 = threading.Thread(target=run,args=("t8",))
t9 = threading.Thread(target=run,args=("t9",))
t10 = threading.Thread(target=run,args=("t10",))

t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t6.start()
t7.start()
t8.start()
t9.start()
t10.start()

t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
t6.join()
t7.join()
t8.join()
t9.join()
t10.join()
end_time = time.time()
print(end_time - start_time)

'''

#定时器
import threading
def deal_task(n):
    print('%s 我被执行了~' % n)
t = threading.Timer(3, deal_task, args=(10,))
t.start()

#Event
'''
import threading,time
e = threading.Event()
def hld():
    print("红灯")
    time.sleep(5)
    e.set()
    print("绿灯")
def car_run(name):
    print(name,"迎面驶来")
    e.wait()
    print(name,"继续前进")

for i in range(10):
    t = threading.Thread(target=car_run,args=(i,))
    t.start()

h = threading.Thread(target=hld,)
h.start()
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值