Thread Based Parallelism - Thread in a Subclass
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的更多相关文章
- Thread Based Parallelism - Thread Synchronization With Lock
Thread Based Parallelism - Thread Synchronization With Lock import threading shared_resource_with_lo ...
- Thread Based Parallelism - Thread Synchronization With a Condition
Thread Based Parallelism - Thread Synchronization With a Condition from threading import Thread, Con ...
- 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别
JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...
- Thread.sleep( ) vs Thread.yield( )
Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...
- Thread系列之Thread.Sleep(0)
线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...
- PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...
- 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 ...
- Thread thread2 = new Thread()
Thread thread2 = new Thread() { @Override public void run() { test.function(); } }; thread1.start(); ...
- Mysql thread 与 OS thread
测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...
随机推荐
- Some collections were archived because you’ve reached the shared requests limits.错误解决
今天打开我的postman 发现我的一个collection不见了,左下角出现一个提示, Some collections were archived because you’ve reached t ...
- Quartz定时任务整理
一. 介绍 Quartz是一个开源的定时任务调度框架,这里就不详细介绍了,我们直入主题Quartz主要由三部分组成 任务:JobDetail 触发器:Trigger,(分两类:SimpleTrigge ...
- 权限认证基础:区分Authentication,Authorization以及Cookie、Session、Token
1. 认证 (Authentication) 和授权 (Authorization)的区别是什么? 这是一个绝大多数人都会混淆的问题.首先先从读音上来认识这两个名词,很多人都会把它俩的读音搞混,所以我 ...
- Spring Boot 事务的使用
Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactio ...
- python requests 库 首次使用
安装requests库 执行pip3 install requests 使用resquests库获取百度网站首页 打开python idle终端.以python3为例,在终端执行python3并回车. ...
- vue拦截器
1.在路由添加 meta:{ requireAuth:true } 完整 { path: '/xx', name: 'xx', component: xx, meta:{ requireAuth:tr ...
- Java电商支付系统实战(一)- 简介
现如今,支付成为热点 对于电商业务,这都是不可或缺的 核心功能剖析 下单->支付 nginx 将用户请求反向代理到我们编写的电商系统 = 下单 之后,点击支付跳转到支付系统,最后对接 通过跳转将 ...
- 美食家App开发日记3
由于个人原因,感觉Android的学习特别复杂,初次接触,实在难以完成最初设想,所以将最初的设想做减法. 今天学习了ListView控件,将图片和美食名字使用ListView界面显示出来,并学习提升L ...
- python 抓一下 循环的访问也可以
#!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:urllib2-header.py import urllib2 ...
- php--->cookie和session
cookie和session cookie和session理解 HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有 ...