import threading
import time
def fun1(a):
for i in range(10):
print(threading.current_thread().getName(),i)
time.sleep(a)
def fun2(a):
for i in range(65,76):
print(threading.current_thread().getName(),chr(i))
time.sleep(a)
# thread1 = threading.Thread(target=fun1,args=(1,),name="线程一")
# thread2 = threading.Thread(target=fun2,args=(1,),name="线程线程二")
# thread1.start()
# thread2.start()
class MyThread1(threading.Thread):
def __init__(self,a,name):
# threading.Thread.__init__(self,args=(a,))
super().__init__(name=name)
self.a = a
def run(self):
fun1(self.a)
class MyThread2(threading.Thread):
def __init__(self,a,name):
# threading.Thread.__init__(self,args=(a,))
super().__init__(name=name)
self.a = a
def run(self):
fun2(self.a)
thread1 = MyThread1(0.1,"线程一")
thread1.start()
thread2 = MyThread1(0.1,"线程二")
thread2.start()
import threading
def fun1():
for i in range(10):
print(threading.current_thread().getName(),i)
if i == 5:
thread2 = threading.Thread(target=fun2,name="线程二")
thread2.start()
thread2.join()
def fun2():
for i in range(65,76):
print(threading.current_thread().getName(),chr(i))
thread1 = threading.Thread(target=fun1,name="线程一")
thread1.start()
import threading
lock = threading.Lock()
ticket = 100
def seal():
global ticket
global lock
lock.acquire()
while ticket>=0:
print(threading.current_thread().getName(),ticket)
ticket-=1
lock.release()
for i in range(3):
thread = threading.Thread(target=seal,name="线程{}==>".format(i+1))
thread.start()
培训代码多线程
猜你喜欢
转载自blog.csdn.net/weixin_38241876/article/details/88763835
今日推荐
周排行