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

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. leetcode-159周赛-5232-替换子串得到平衡字符串*

    题目描述: 方法: 另: class Solution: def balancedString(self, s: str) -> int: n, req = len(s), len(s) // ...

  2. bootstrapValidator--表单校验

    关于表单校验 要依次引入 <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css"& ...

  3. 01退背包——bzoj2287

    退背包就是限制某一件物品不可取的方案数 先做出无限制的方案数,然后对于当前不可取的物品,dp2[j]表示不取改物品情况下,取得体积为j的方案数 有状态方程 dp2[j]=dp1[j]-dp2[j-w[ ...

  4. eclipse打包插件net.sf.fjep.fatjar

    eclipse打包插件安装 1)将net.sf.fjep.fatjar_0.0.32.jar拷贝到eclipse安装目录中的plugins目录下,然后重启eclipse即可. 软件获取方式: 链接:h ...

  5. go beego

    一. 引入 go get github.com/astaxie/beego go get gitgub.com/beego/bee go get -u gitxxx.... 更新框架 编写 packa ...

  6. 【Web】浅析JQuery的apply(), call(), bind()方法

    原文地址:https://blog.csdn.net/whuzxq/article/details/64166253 由于在理解this的用法的时候多次出现了这几个方法,个人对这几个方法理解的不是很透 ...

  7. [kuangbin带你飞]专题一 简单搜索 - M - 非常可乐

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  8. mybatis浅显认识

    mybatis主配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configu ...

  9. spring配置hibernate的sessionFactory

    1.首先通过dataSource来配置sessionFactory <!--读入配置文件 --> <bean id="propertyConfigurer" cl ...

  10. scp 传输下载

    利用scp传输文件 1.从服务器下载文件 scp username@servername:/path/filename /tmp/local_destination 例如scp codinglog@1 ...