不断的练,加深记忆吧。

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import threading
  5. import time
  6.  
  7. exitFlag = 0
  8.  
  9. def first_function():
  10. print (threading.currentThread().getName() + \
  11. str(' is Starting \n'))
  12. time.sleep(2)
  13. print (threading.currentThread().getName() + \
  14. str(' is Exiting \n'))
  15.  
  16. def second_function():
  17. print (threading.currentThread().getName() + \
  18. str(' is Starting \n'))
  19. time.sleep(2)
  20. print (threading.currentThread().getName() + \
  21. str(' is Exiting \n'))
  22.  
  23. def third_function():
  24. print (threading.currentThread().getName() + \
  25. str(' is Starting \n'))
  26. time.sleep(2)
  27. print (threading.currentThread().getName() + \
  28. str(' is Exiting \n'))
  29.  
  30. class myThread(threading.Thread):
  31. def __init__(self, threadID, name, counter):
  32. threading.Thread.__init__(self)
  33. self.threadID = threadID
  34. self.name = name
  35. self.counter = counter
  36. def run(self):
  37. print("Starting " + self.name)
  38. print_time(self.name, self.counter, 5)
  39. print("Exiting " + self.name)
  40.  
  41. def print_time(threadName, delay, counter):
  42. while counter:
  43. if exitFlag:
  44. thread.exit()
  45. time.sleep(delay)
  46. print("%s: %s" % (threadName, time.ctime(time.time())))
  47. counter -= 1
  48.  
  49. if __name__ == "__main__":
  50. t1 = threading.Thread\
  51. (name='first_function', target=first_function)
  52. t2 = threading.Thread\
  53. (name='second_function', target=second_function)
  54. t3 = threading.Thread\
  55. (name='third_function', target=third_function)
  56.  
  57. t1.start()
  58. t2.start()
  59. t3.start()
  60. t1.join()
  61. t2.join()
  62. t3.join()
  63.  
  64. thread1 = myThread(1, "Thread-1", 1)
  65. thread2 = myThread(2, "Thread-2", 2)
  66. thread1.start()
  67. thread2.start()
  68. thread1.join()
  69. thread2.join()
  70. print("Exiting Main Thread")

基于类和基于函数的python多线程样例的更多相关文章

  1. Atitit.prototype-base class-based  基于“类” vs 基于“原型”

    Atitit.prototype-base class-based  基于“类” vs 基于“原型” 1. 基于“类” vs 基于“原型”1 2.  对象的产生有两种基本方式.一种是以原型(proto ...

  2. gtk+3.0的环境配置及基于gtk+3.0的python简单样例

    /*********************************************************************  * Author  : Samson  * Date   ...

  3. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  4. java多线程样例

    这里我们做一个完整的样例来说明线程产生的方式不同而生成的线程的差别: package debug; import java.io.*;import java.lang.Thread; class My ...

  5. Python word_cloud 样例 标签云系列(三)

    转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...

  6. 函数指针使用演示样例(參考Linux-内核代码)

    本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...

  7. 维特比算法(Viterbi)及python实现样例

    维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...

  8. Boost Python官方样例(三)

    导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...

  9. Boost Python官方样例(一)

    配置环境 $ cat /etc/os-release NAME="Ubuntu" VERSION="16.04 LTS (Xenial Xerus)" ID=u ...

随机推荐

  1. Ngrok搭建服务器

    一.ngrok简介及作用 ngrok 是一款用go语言开发的开源软件,它是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.下图简述了ngrok的原理. ngrok 可 ...

  2. SQL Server 2008 R2安装图解教程

    一.下载SQL Server 2008 R2安装文件 cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso 二.将安装文件刻录成光盘 ...

  3. 2016年11月6日--form表单验证和事件、正则表达式

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  4. iOS关于UILabel 基本属性 背景图片 背景色

    [代码] iOS关于UILabel 基本属性 背景图片 背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  5. MYSQL 的错误Incorrect information in file: '.\test\stuff.frm

    用eos 重新数据库初始化 会出现这个错误 然后 把 mysql 的 两个日志文件删除就行.第一次不知道怎么g搞,花了一下午的时间重装了mysql.5.0. 删除文件 >>>> ...

  6. 【GoLang】golang 面向对象编程 & 面向接口编程

    005.面向对象&接口编程 1 面向函数编程 1.1 将数据作为参数传递到函数入参 1.2 对象与函数是分离的 2 面向对象编程 2.1 使用者看起来函数作为对象的属性而非参数 2.2 函数属 ...

  7. js隐藏div和class

    <style type="text/css"> //div用点//class# .footer {  display:none;  } #footer {  displ ...

  8. mysql的innodb中事务日志ib_logfile

    mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...

  9. angular.js初探

    2015年7月27日 22:26:35 星期一 用在我论坛里的小栗子: 先列出来一级回帖, 点击帖子前边的"查看回复"按钮无刷新的去请求该帖子的所有回复 首先要引用js文件, 我这 ...

  10. 100个Github上Android开源库

    项目名称 项目简介 1. react-native 这个是 Facebook 在 React.js Conf 2015 大会上推出的基于 JavaScript 的开源框架 React Native, ...