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- ...
随机推荐
- debian8最小化安装,字符界面的中文配置
一.现象: debian8最小化安装以后,字符界面的中文显示乱码. 二.解决 1. 安装locales apt-get install locales 2. 配置locales dpkg-reconf ...
- Arduino智能小车--仅仅是随便一搞
在某宝宝买的智能小车,挺精致的,开心的连接上打印机的线,结果port都没有反应, 查了一下发现是少了驱动,博主用的mac os10.12.3 CH34x_Install_V1.4.pkg 安装好之后我 ...
- DVWA手记——取消登录
DVWA在渗透测试方面表现相当不错,可以自定义高中低的安全级别.同事为了测试一个小工具,只好取消登录认证.本以为Config可以设置,结果没有,只好自己动手了——才能风衣足食. 更改文件:\dvwa\ ...
- C#基础第五天-作业答案-用DataTable制作名片集
.DataTable 实现 DataTable PersonCard = new DataTable(); //创建一个DataTable DataTable PersonCardCopy = new ...
- 请教Mysql如何删除 不包含 某些字符的记录
删除包含指定字符的记录 delete from `表` where `字段` like '%指定字符1%' or like '%指定字符2%' or like '%指定字符3%' 删除不包含指定字符的 ...
- 在ubuntu16安装supervisor 并 启动 .net core.
1. 安装supervisor apt-get install supervisor 2.新建supervisor配置文件.使用以下命令在linux 命令行 echo_supervisord_conf ...
- ace admin后台模板
http://ace.jeka.by/index.html 下载: http://www.daimajiayuan.com/plus/download.php?open=0&aid=18152 ...
- lua -- debug
framework.debug 调试支持 ~~ echo 功能同 print. 格式: echo(值, [值, 值, ...]) ~~ printf 按照特定格式输出. 格式: printf(格式字符 ...
- 把虚拟教练带回家,「EuMotus」想用AI实现高精度运动反馈系统
https://36kr.com/p/5089139.html 无需穿戴设备,只需一个红外摄像头和+已安装好EuMotus专利软件的手提电脑 由政府主导的高达2200亿美金的健身与运动支出,15%的健 ...
- [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.
Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...