Thread Based Parallelism - Thread in a Subclass
  
1 import threading
import time exit_Flag = 0 class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name + "\n")
print_time(self.name, self.counter, 5)
print ("Exiting " + self.name + "\n") def print_time(threadName, delay, counter):
while counter:
if exit_Flag:
thread.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1 if __name__ == '__main__':
# Create two threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2) # Start the Threads created
thread1.start()
thread2.start() # Wait for all thread to complete
thread1.join()
thread2.join() print ("Exiting Main Thread") Output,
Starting Thread-1
Starting Thread-2 Thread-1: Thu Feb 8 15:08:47 2018
Thread-1: Thu Feb 8 15:08:48 2018
Thread-2: Thu Feb 8 15:08:48 2018
Thread-1: Thu Feb 8 15:08:49 2018
Thread-2: Thu Feb 8 15:08:50 2018
Thread-1: Thu Feb 8 15:08:50 2018
Thread-1: Thu Feb 8 15:08:51 2018
Exiting Thread-1 Thread-2: Thu Feb 8 15:08:52 2018
Thread-2: Thu Feb 8 15:08:54 2018
Thread-2: Thu Feb 8 15:08:56 2018
Exiting Thread-2 Exiting Main Thread

最新文章

  1. 2D、3D形变
  2. UIButton添加倒计时
  3. Linux umount设备时出现device is busy解决方法
  4. ASP.NET MVC+Bootstrap个人博客之修复UEditor编辑时Bug(四)
  5. Android开发UI之Navigation Drawer
  6. GUI编程笔记(java)06:GUI窗体添加按钮并对按钮添加事件案例
  7. 关于基本视频播放的Demo
  8. MySQL 报错 _DATA_TYPE_INVALID_
  9. hdu 1207 四柱汉诺塔
  10. [转]gitlab cicd (二)系列之安装git-runner rpm安装方式
  11. CSS3之box-sizing属性
  12. BZOJ.5461.[PKUWC2018]Minimax(DP 线段树合并)
  13. Leetcode——171.Excel表列序号【水题】
  14. Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)
  15. 使用 kbmmw 的ORM开发纯REST数据库访问服务
  16. BZOJ5092 分割序列(贪心)
  17. js & enter
  18. Octopress + GitHub Page 搭建个人博客
  19. Hadoop2的FN安装(federated namespace)
  20. Form表单元素

热门文章

  1. 《【面试突击】— Redis篇》--Redis都有哪些数据类型?分别在哪些场景下使用比较合适?
  2. 你的应用安全吗? ——用Xray和Synk保驾护航
  3. Java BIO NIO 与 AIO
  4. GB国标编码的程序出现乱码
  5. Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解
  6. 权限认证基础:区分Authentication,Authorization以及Cookie、Session、Token
  7. Python的re模块,正则表达式用法详解,正则表达式中括号的用法
  8. hdu6601 主席树
  9. js 获取元素坐标 和鼠标点击坐标
  10. Java容器解析系列(11) HashMap 详解