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

Thread Based Parallelism - Thread in a Subclass的更多相关文章

  1. Thread Based Parallelism - Thread Synchronization With Lock

    Thread Based Parallelism - Thread Synchronization With Lock import threading shared_resource_with_lo ...

  2. Thread Based Parallelism - Thread Synchronization With a Condition

    Thread Based Parallelism - Thread Synchronization With a Condition from threading import Thread, Con ...

  3. 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别

    JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...

  4. Thread.sleep( ) vs Thread.yield( )

    Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...

  5. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...

  6. PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别

    链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...

  7. Part 92 Significance of Thread Join and Thread IsAlive functions

    Thread.Join & Thread.IsAlive functions Join blocks the current thread and makes it wait until th ...

  8. Thread thread2 = new Thread()

    Thread thread2 = new Thread() { @Override public void run() { test.function(); } }; thread1.start(); ...

  9. Mysql thread 与 OS thread

    测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...

随机推荐

  1. Java 数据结构快速入门

    数据结构:栈 简介 栈(stack),又称堆栈,它是运算受限的线性表. 限制 栈(stack)的限制是仅允许在标的一端进行插入和删除操作,不允许在其他任何位置进行添加.查找.删除等操作. 采用该结构的 ...

  2. Ubuntu TTY 字体大小 目录颜色 中文乱码 设置

    TTY有很多不舒服的地方,没有中文 目录是深蓝色的看不清楚 字太小. 通过设置让终端更舒服一些. 进入TTY 从桌面进入TTY: 按`Ctrl+Alt+F[1-9] 有的版本图形界面在F7和F8,有的 ...

  3. 七牛云上传视频并截取第一帧为图片(js实现)

    本文出自APICloud官方论坛, 感谢论坛版主 东冥羽的分享. 七牛云上传视频并截取第一帧作为视频的封面图. 使用js上传,模块videoPlayer截取第一帧(有专门的截图模块,但是我使用的有点问 ...

  4. SpringBoot + Apache Shiro权限管理

    之前配置过Spring + SpringMVC + JPA + Shiro后台权限管理 + VUE前台登录页面的框架,手动配置各种.xml,比较繁琐,前几天写了个SpringBootShiro的Dem ...

  5. 【PCIE-3】---PCIE设备的枚举扫描(经典好文)

    前面两个小节大致总结了下PCIE的基本知识,算是扫盲篇吧.本文主要总结PCIE设备的枚举扫描过程,此部分才是PCIE模块的重点,无论是在BIOS下还是系统驱动下都会用到. 按照国际惯例,先列问题: 1 ...

  6. vue学习笔记1:el 与 data

    一.vue介绍 vue是目前三大主流框架之一(React.Angular.Vue) vue特点: 易用 灵活 高效 vue官网:官网链接 二,知识点 vue实例选项: el 注:不能 让el直接管理h ...

  7. Activiti服务任务(serviceTask)

    Activiti服务任务(serviceTask) 作者:Jesai 都有一段沉默的时间,等待厚积薄发 应用场景: 当客户有这么一个需求:下一个任务我需要自动执行一些操作,并且这个节点不需要任何的人工 ...

  8. Docker学习(十一)Docker系列结束-新的开始K8S

    Docker学习(十一)Docker系列结束-新的开始K8S 标签(空格分隔): docke k8s Docker系列结束 上一篇讲到使用docker官方提供的容器编排工具docker-compose ...

  9. Filder配置及使用教程

    https://www.cnblogs.com/woaixuexi9999/p/9247705.html

  10. 7.Java帝国的诞生

    1972年,C诞生,而Java是1995年诞生的.它贴近硬件,有汇编语言的特性,运行极快,效率极高.早期,用在操作系统.编译器.数据库.网络系统等.但它有两把沉重的枷锁一直在程序员身上,那就是指针和内 ...