Python 创建线程的方法
使用的是 threading 模块
代码如下:
1 #!/usr/bin/python3
2
3 import threading
4 import time
5
6 exitFlag = 0
7 // 创建线程的类
8 class myThead (threading.Thread):
9 def __init__(self, threadID, name, counter):
10 threading.Thread.__init__(self)
11 self.threadID = threadID
12 self.name = name
13 self.counter = counter
14 def run(self):
15 threadLock.acquire(); // 线程同步
16 print("start function : "+ self.name)
17 print_time(self.name, self.counter, 5)
18 print("exit function :" + self.name)
19 threadLock.release() // 线程同步释放
20
21 // 线程调用的函数
22 def print_time(threadName, delay, counter):
23 while counter:
24 if exitFlag:
25 threadName.exit()
26 time.sleep(delay)
27 print ("%s : %s" % (threadName, time.ctime(time.time())))
28 counter -= 1
29
30 threadLock = threading.Lock()
31 threads = []
32
33 thread1 = myThead(1, "Thread-1", 1); // 创建线程的实体
34 thread2 = myThead(2, "Thread-2", 2);
35 thread3 = myThead(3, "Thread-3", 3);
36
37 thread1.start()
38 thread2.start()
39 thread3.start()
40
41 threads.append(thread1); // 加入线程数组
42 threads.append(thread2);
43 threads.append(thread3);
44
45 for t in threads:
46 t.join(); // 线程执行
47
48 print("exit main process");
49
Python 创建线程的方法的更多相关文章
- java创建线程的方法
1.1 创建线程 1.1.1 无返回值的线程创建 package com.first; public class ThreadTest { public static void ma ...
- 【Java 线程的深入研究1】Java 提供了三种创建线程的方法
Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...
- 程序员:java中直接或间接创建线程的方法总结
在java开发中,经常会涉及多线程的编码,那么通过直接或间接创建线程的方法有哪些?现整理如下: 1.继承Thread类,重写run()方法 class Worker extends Thread { ...
- Python创建线程
Python 提供了 _thread 和 threading 两个模块来支持多线程,其中 _thread 提供低级别的.原始的线程支持,以及一个简单的锁,正如它的名字所暗示的,一般编程不建议使用 th ...
- Java 多线程 - 创建线程的方法 + Executors.newXXXThreadPool()缺点
java中创建线程的三种方法以及区别: https://www.cnblogs.com/3s540/p/7172146.html 通过Executor 的工具类,创建三种类型的普通线程池: https ...
- python创建数组的方法
一 直接定义法: 1.直接定义 matrix=[0,1,2,3] 2.间接定义 matrix=[0 for i in range(4)] print(matrix) 二 Numpy方法: Numpy内 ...
- Java 创建线程的方法
为了偷懒少敲几个字这里我写了一个Util类: package test; public class Util { static void println() {System.out.println() ...
- python 创建flask项目方法
Flask是一个基于Python的web框架,它的设计目的是提供Web开发所需的最小功能子集. Flask与别的框架(尤其是采用其他编程语言的框架)的不同之处在于:它没有绑定诸如数据库查询或者表单处理 ...
- Python创建virtualenv虚拟环境方法
一.通过virtualenv软件创建 安装: -pip3 install virtualenv 创建虚拟环境: -(1)virtualenv wannings-ms- ...
随机推荐
- Linux/Unix 新手和专家教程
你正在找一些高质量的Linux 和 UNIX 的教程吗?如果是,这篇文章会告诉你到哪去找到这些教程.这里我们将给出超过30个相当的不错的 Linux 和 UNIX 在线的教程. 需要大家注意的是,他们 ...
- JAVA Socket编程和C++ Socket编程有什么不同
原文链接: http://zhidao.baidu.com/link?url=16TEzhom2Nr8x1_2uTRp-e2pgZRgS5nW5ywtRX2XLHbtLOG8btif5DTyP85jf ...
- SQL 给字符串补0
第一种方法: right('00000'+cast(@count as varchar),5) 其中'00000'的个数为right函数的最后参数,例如这里是5,所以有5个0 @count就是被格式化 ...
- OLTP和OLAP有何区别?
OLTP即联机事务处理,就是我们经常说的关系数据库,意即记录即时的增.删.改.查,就是我们经常应用的东西,这是数据库的基础:OLAP即联机分析处理,是数据仓库的核心部心,所谓数据仓库是对于大量已经由O ...
- Java 8 – How to format LocalDateTime
Few examples to show you how to format java.time.LocalDateTime in Java 8. 1. LocalDateTime + DateTim ...
- 如何在ppt或word中添加高亮代码?
如何在ppt或word中添加高亮代码? 问题: 如何ppt中添加带有语法高亮的程序代码,就如同下面的形式(在ppt嵌入高亮代码,且可以编辑,带有行号) 详细的要求如下: 1.代码带有语法高亮. 2.结 ...
- 关于Big Endian 和 Little Endian
Big Endian 和 Little Endian 一.字节序 来自:http://ayazh.gjjblog.com/archives/1058846/ 谈到字节序的问题,必然牵涉到两大CPU派系 ...
- unity, Animator.ResetTrigger
解: 正确的写法应该是:Animator.SetTrigger("unfoldTrigger")Animator.ResetTrigger("unfoldTrigger& ...
- Android 架构师技能图谱(转载)
架构与设计 设计模式 重构 技术选型 特性 可用性 性能 包大小 方法数 文档 技术支持 UI架构模式 MVC MVP MVVM 研发工具 集成开发环境 Android Studio Sublime ...
- npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script build`?
跑npm build结果如下: npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script ...