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. python 多进程处理图像,充分利用CPU

    默认情况下,Python程序使用一个CPU以单个进程运行.不过如果你是在最近几年配置的电脑,通常都是四核处理器,也就是有8个CPU.这就意味着在你苦苦等待Python脚本完成数据处理工作时,你的电脑其 ...

  2. 用python搭一个超简易的文件服务器

    这个文件服务器纯粹是在学习python cgi编程时,顺便玩玩而已,因为搭文件服务器的话完全可以linux,简单方便,这里就是随便玩玩,功能也就是只能下载文件 1.登录页面,做个简单验证 新建一个ht ...

  3. 三个css属性 设置文字竖直居中

    display: flex; justify-content:center; align-items:Center;

  4. mysql高级复习

    MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.可以得到索引的本质:索引是数据结构,可以简单理解为“排好序的快速查找数据结构”. 数据本身之外,数据库还维护着一 ...

  5. Jenkins-正则表达式提取Cookie

    场景描述:用户登录查询数据:登录时响应头返回Cookie,将Cookie使用正则表达式提取出来,在下一个请求“查询数据”时,在请求头中使用此Cookie 1.添加“登录”http请求 2.在后置处理器 ...

  6. Spring Boot自动装配

    前言 一些朋友问我怎么读源码,这篇文章结合我看源码时候一些思路给大家聊聊,我主要从这三个方向出发: 确定目标,这个目标要是一个具体,不要一上来我要看懂Spring,这是不可能的,目标要这么来定,比如看 ...

  7. Scrapy持久化(items+pipelines)

    一.items保存爬取的文件 items.py import scrapy class QuoteItem(scrapy.Item): # define the fields for your ite ...

  8. python __init__()类构造方法

    构造方法用于创建对象时使用,每当创建一个类的实例对象时,python解释器都会自动调用它. class Person: def __init__(self): print("调用构造方法&q ...

  9. [bzoj4524] [loj#2047] [Cqoi2016] 伪光滑数

    Description 若一个大于 \(1\) 的整数 \(M\) 的质因数分解有 \(k\) 项,其最大的质因子为 \(Ak\) ,并且满足 \(Ak^K \leq N\) , \(Ak<12 ...

  10. 美食家App开发日记1

    前期一直在看第一行代码Android,这本书感觉讲基础讲得特别细致. 百看不如一试. 因为刚刚接触Android,没办法做到想写什么功能就直接一下写好,只能从最开始基础的控件使用开始练习. 所以一直在 ...