博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thread Based Parallelism - Thread in a Subclass
阅读量:5359 次
发布时间:2019-06-15

本文共 1879 字,大约阅读时间需要 6 分钟。

 
  Thread Based Parallelism - Thread in a Subclass
    1     import threading 2     import time 3  4     exit_Flag = 0 5  6     class myThread (threading.Thread): 7         def __init__(self, threadID, name, counter): 8             threading.Thread.__init__(self) 9             self.threadID = threadID10             self.name = name11             self.counter = counter12         def run(self):13             print ("Starting " + self.name + "\n")14             print_time(self.name, self.counter, 5)15             print ("Exiting " + self.name + "\n")16 17     def print_time(threadName, delay, counter):18         while counter:19             if exit_Flag:20                 thread.exit()21             time.sleep(delay)22             print ("%s: %s" % (threadName, time.ctime(time.time())))23             counter -= 124 25     if __name__ == '__main__':26         # Create two threads27         thread1 = myThread(1, "Thread-1", 1)28         thread2 = myThread(2, "Thread-2", 2)29 30         # Start the Threads created31         thread1.start()32         thread2.start()33 34         # Wait for all thread to complete35         thread1.join()36         thread2.join()37 38         print ("Exiting Main Thread")39 40     Output,41         Starting Thread-142         Starting Thread-243 44         Thread-1: Thu Feb  8 15:08:47 201845         Thread-1: Thu Feb  8 15:08:48 201846         Thread-2: Thu Feb  8 15:08:48 201847         Thread-1: Thu Feb  8 15:08:49 201848         Thread-2: Thu Feb  8 15:08:50 201849         Thread-1: Thu Feb  8 15:08:50 201850         Thread-1: Thu Feb  8 15:08:51 201851         Exiting Thread-152 53         Thread-2: Thu Feb  8 15:08:52 201854         Thread-2: Thu Feb  8 15:08:54 201855         Thread-2: Thu Feb  8 15:08:56 201856         Exiting Thread-257 58         Exiting Main Thread

 

转载于:https://www.cnblogs.com/zzyzz/p/8431675.html

你可能感兴趣的文章
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>
Python之旅Day14 JQuery部分
查看>>
core--线程池
查看>>
redux-effect
查看>>
他山之石:加载图片的一个小问题
查看>>
shell - 常识
查看>>
linux下编译复数类型引发的错误:expected unqualified-id before '(' token
查看>>
codeforces 1041A Heist
查看>>
Spring Cloud Stream消费失败后的处理策略(三):使用DLQ队列(RabbitMQ)
查看>>
PKUWC2018 5/6
查看>>
As-If-Serial 理解
查看>>
洛谷P1005 矩阵取数游戏
查看>>
在Silverlight中使用HierarchicalDataTemplate为TreeView实现递归树状结构
查看>>
无线通信基础(一):无线网络演进
查看>>
关于python中带下划线的变量和函数 的意义
查看>>
linux清空日志文件内容 (转)
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
Ajax : load()
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>