# 主线程等待所有子线程结束才结束
import threading
from time import sleep,ctime def sing():
for i in range(3):
print("正在唱歌---%d"%(i))
sleep(2)
def dance():
for i in range(3):
print("正在跳舞---%d" % (i))
sleep(2)
if __name__ == '__main__':
print("----开始----%s"%(ctime()))
t_sing = threading.Thread(target=sing)
t_dance = threading.Thread(target=dance)
t_sing.start()
t_dance.start()
print("----结束----%s"%(ctime()))

#查看线程数量
import threading
from time import sleep,ctime def sing():
for i in range(3):
print("正在唱歌---%d"%i)
sleep(1)
def dance():
for i in range(3):
print("正在跳舞---%d"%i)
sleep(i)
if __name__ == '__main__':
t_sing = threading.Thread(target=sing)
t_dance = threading.Thread(target=dance)
t_sing.start()
t_dance.start()
while True:
length = len(threading.enumerate())
print("当前运行的线程数为:%d"%(length))
if length<= 1:
break
sleep(0.5)

import threading
import time class MyThread(threading.Thread):
# 重写 构造方法
def __init__(self, num, sleepTime):
threading.Thread.__init__(self)
self.num = num
# 类实例不同,num值不同
self.sleepTime = sleepTime def run(self):
self.num += 1
time.sleep(self.sleepTime)
print('线程(%s),num=%d' % (self.name, self.num)) if __name__ == '__main__':
mutex = threading.Lock()
t1 = MyThread(100, 3)
t1.start()
t2 = MyThread(200, 1)
t2.start()

import threading
from time import sleep g_num = 1 def test(sleepTime):
num = 1 #num为局部变量
sleep(sleepTime)
num += 1
global g_num #g_num为全局变量
g_num += 1
print('---(%s)--num=%d --g_num=%d' % (threading.current_thread(), num,g_num)) t1 = threading.Thread(target=test, args=(3,))
t2 = threading.Thread(target=test, args=(1,)) t1.start()
t2.start()

import threading
import time class MyThread1(threading.Thread):
def run(self):
if mutexA.acquire():
print("A上锁了")
mutexA.release()
time.sleep(2)
if mutexB.acquire():
print("B上锁了")
mutexB.release()
mutexA.release() class MyThread2(threading.Thread):
def run(self):
if mutexB.acquire():
print("B上锁了")
mutexB.release()
time.sleep(2)
if mutexA.acquire():
print("A上锁了")
mutexA.release()
mutexB.release()
# 先看B是否上锁,然后看A是否上锁
mutexA = threading.Lock()
mutexB = threading.Lock() if __name__ == "__main__":
t1 = MyThread1()
t2 = MyThread2()
t1.start()
t2.start()

多线程threading的执行顺序(不确定)
# 只能保证都执行run函数,不能保证执行顺序和开始顺序
import threading
import time class MyThread(threading.Thread):
def run(self):
for i in range(3):
time.sleep(1)
msg = "I'm "+self.name+' @ '+str(i)
print(msg)
def test():
for i in range(5):
t = MyThread()
t.start()
if __name__ == '__main__':
test()

多线程threading的注意点
import threading
import time class MyThread(threading.Thread):
# 重写threading.Thread类中的run方法
def run(self):
for i in range(3):#开始线程之后循环三次
time.sleep(1)
msg = "I'm "+self.name+'@'+str(i)
# name属性是当前线程的名字
print(msg)
if __name__ == '__main__':
t = MyThread()#使用threading.Thread的继承类
t.start()#继承线程之后要开始运行 start方法

2020-05-07

最新文章

  1. Python Locust对指定网站“一键压测”
  2. 2016暑假多校联合---Death Sequence(递推、前向星)
  3. flask表单提交的两种方式
  4. 5 分钟上手 ECharts
  5. Android 开关按钮切换,类似于iphone 效果,view实现
  6. python 打包exe注意的问题
  7. POJ 2406 Power Strings KMP运用题解
  8. 229. Majority Element II My Submissions Question
  9. asp.net mvc 实现记忆返回的功能
  10. IsNullOrEmpty和s == null || s.Length == 0哪个快
  11. Attach()函数和Detach()函数的作用
  12. 你不知道的JavaScript上卷笔记
  13. PHP加水印代码 支持文字和图片水印
  14. lesson - 1 aming
  15. obj-c编程15[Cocoa实例03]:MVC以及归档化示例
  16. 为什么在STM32F429工程配置中需要预先定义USE_STDPERIPH_DRIVER和STM32F429_439xx?
  17. 通过反射将request中的参数封装到对象中
  18. 学习类后回顾pickle及collections模块应用场景优化
  19. instanceof判断的对象可以是接口
  20. linux 各项分布(个人记录)

热门文章

  1. Oracle12c安装记录(centos6.5,命令行)
  2. Mac上使用Docker Desktop启动Kubernetes,踩坑后终于搞掂
  3. js 字符串转方法,this域绑定
  4. [PA2015]Siano 单调栈
  5. 状压DP之吃奶酪
  6. MySQL 快速删除大量数据(千万级别)的几种实践方案
  7. Howdoo欢迎Mitel成为内容发布支持者
  8. 利用docker部署oxidized网络设备备份系统
  9. 【初学】Spring源码笔记之零:阅读源码
  10. 数据结构之二叉搜索树(BST)--JavaScript实现