利用阻塞的时间空闲去执行另一个子线程

import threading
from time import ctime, sleep def music(func):  
  for i in range(2):
    print(func, ctime())      # 1 执行  # 4 执行  
    sleep(1)       # 阻塞
    print("end music", ctime())  # 3 执行  # 5 执行 def move(func):
  for i in range(2):
    print(func, ctime())      # 2 执行  # 7 执行
    sleep(5)       # 阻塞
    print("end move", ctime())  # 6 执行  # 8 执行 threads=[]
t1 = threading.Thread(target=music,args=("小苹果",))
threads.append(t1)
t2 = threading.Thread(target=move,args=("华尔街之狼",))
threads.append(t2) if __name__ == "__main__":   
  for t in threads:    # 遍历执行两个子线程
    t.start()      
# 整体执行时间为10秒

代码运行结果:

小苹果 Fri Sep  7 16:19:09 2018
华尔街之狼 Fri Sep 7 16:19:09 2018
end music Fri Sep 7 16:19:10 2018
小苹果 Fri Sep 7 16:19:10 2018
end music Fri Sep 7 16:19:11 2018
end move Fri Sep 7 16:19:14 2018
华尔街之狼 Fri Sep 7 16:19:14 2018
end move Fri Sep 7 16:19:19 2018    

threading线程例子 (27-08)的更多相关文章

  1. python并发编程之threading线程(一)

    进程是系统进行资源分配最小单元,线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.进程在执行过程中拥有独立的内存单元,而多个线程共享内存等资源. 系列文章 py ...

  2. Python学习笔记--threading线程

    通过线程来实现多任务并发.提高性能.先看看例子. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2020-03-02 21:10:39 ...

  3. pythonl练习笔记——threading线程中的事件Event

    1 事件Event 使用方法:e = threading.Event() Event对象主要用于线程间通信,确切地说是用于主线程控制其他线程的执行. Event事件提供了三个方法:wait等待.cle ...

  4. DELPHI线程例子-FC

    {优秀的数据库应用应当充分考虑数据库访问的速度问题.通常可以通过优化数据库.优化 查询语句.分页查询等途径收到明显的效果.即使是这样,也不可避免地会在查询时闪现一个带有 SQL符号的沙漏,即鼠标变成了 ...

  5. 多进程 multiprocessing 多线程Threading 线程池和进程池concurrent.futures

    multiprocessing.procsess 定义一个函数 def func():pass 在if __name__=="__main__":中实例化 p = process( ...

  6. Python Threading 线程/互斥锁/死锁/GIL锁

    导入线程包 import threading 准备函数线程,传参数 t1 = threading.Thread(target=func,args=(args,)) 类继承线程,创建线程对象 class ...

  7. threading线程中的方法(27-11)

    t1.start() # 执行线程 t1.join() # 阻塞 t1.setDaemon(True) #守护线程 threading.current_thread() # 查看执行的是哪一个线程 t ...

  8. import threading线程进程

    cpu在执行一个子线程的时候遇到sleep就会利用这段停顿时间去执行另一个子线程.两个子线程谁先跳出sleep就执行谁. import threadingimport time start = tim ...

  9. java线程例子登山

    Through its implementation, this project will familiarize you with the creation and execution of thr ...

随机推荐

  1. toLocaleString 日期

    new Date().toLocaleString()"2018/5/3 下午3:08:48"

  2. 移动端布局 + iscroll + 滚动事件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  3. 数字IC设计工程师成长之路

    学习的课程 仿真工具VCS实践学习 2019年12月9日-2019年12月23日

  4. react使用阿里爸爸的iconfont时,不展示的问题

    选择使用Unicode时: 正常使用如下,显示也是正常: <i className="iconfont"></i> 使用map去循环时,需将原本的,改成 ...

  5. ASCII 对应表

    { Bin (二进制) Oct (八进制) Dec (十进制) Hex (十六进制) 缩写/字符 解释 0000 0000 00 0 0x00 NUL(null) 空字符 0000 0001 01 1 ...

  6. 线性dp(记忆化搜索)——cf953C(经典好题dag和dp结合)

    非常好的题!和spoj 的 Mobile Service有点相似,用记忆化搜索很容易解决 看了网上的题解,也是减掉一维,刚好可以开下数组 https://blog.lucien.ink/archive ...

  7. 兼容火狐浏览器的select下拉框样式

    经常遇到这样的问题,在使用bootstrap框架的时候select选择框的样式在火狐浏览器上继承的是浏览器本身的样式,跟谷歌等其他的浏览器样式对比很难看,并且很难调整,但是! 好东西来了,现在能调成跟 ...

  8. 微信H5授权登陆

    Controllerpackage com.iimscloud.auth.provider.controller; import org.springframework.beans.factory.a ...

  9. NX二次开发-UFUN求对象的最大边界框UF_MODL_ask_bounding_box

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  10. ionic-CSS:ionic 颜色

    ylbtech-ionic-CSS:ionic 颜色 1.返回顶部 1. ionic 颜色 ionic 提供了很多颜色的配置,当然你可以根据自己的需要自定义颜色. 实例 <ul class=&q ...