• 使用的是 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 创建线程的方法的更多相关文章

  1. java创建线程的方法

    1.1      创建线程 1.1.1     无返回值的线程创建 package com.first; public class ThreadTest { public static void ma ...

  2. 【Java 线程的深入研究1】Java 提供了三种创建线程的方法

    Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...

  3. 程序员:java中直接或间接创建线程的方法总结

    在java开发中,经常会涉及多线程的编码,那么通过直接或间接创建线程的方法有哪些?现整理如下: 1.继承Thread类,重写run()方法 class Worker extends Thread { ...

  4. Python创建线程

    Python 提供了 _thread 和 threading 两个模块来支持多线程,其中 _thread 提供低级别的.原始的线程支持,以及一个简单的锁,正如它的名字所暗示的,一般编程不建议使用 th ...

  5. Java 多线程 - 创建线程的方法 + Executors.newXXXThreadPool()缺点

    java中创建线程的三种方法以及区别: https://www.cnblogs.com/3s540/p/7172146.html 通过Executor 的工具类,创建三种类型的普通线程池: https ...

  6. python创建数组的方法

    一 直接定义法: 1.直接定义 matrix=[0,1,2,3] 2.间接定义 matrix=[0 for i in range(4)] print(matrix) 二 Numpy方法: Numpy内 ...

  7. Java 创建线程的方法

    为了偷懒少敲几个字这里我写了一个Util类: package test; public class Util { static void println() {System.out.println() ...

  8. python 创建flask项目方法

    Flask是一个基于Python的web框架,它的设计目的是提供Web开发所需的最小功能子集. Flask与别的框架(尤其是采用其他编程语言的框架)的不同之处在于:它没有绑定诸如数据库查询或者表单处理 ...

  9. Python创建virtualenv虚拟环境方法

    一.通过virtualenv软件创建 安装:        -pip3 install virtualenv    创建虚拟环境:        -(1)virtualenv wannings-ms- ...

随机推荐

  1. C#winform实现跑马灯

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. SQL触发器 常用语句

    一.创建一个简单的触发器 CREATE TRIGGER 触发器名称 ON 表名 FOR INSERT.UPDATE 或 DELETE AS     T-SQL 语句 注意:触发器名称是不加引号的.   ...

  3. Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)

    源码:http://files.cnblogs.com/android100/StandardCamera2013-10-18.zip 废话不说了,就是加个seekbar,拖动的话能够调节焦距,让画面 ...

  4. Java – How to add days to current date

    1. Calendar.add Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current ...

  5. jmeter 的java请求代码在main方法里面执行

    1.新建一个java请求执行加法类 public class TestDemo { public int Tdemo(int a,int b){ int sum = 0; sum = a+b; ret ...

  6. Git--团队开发必备神器

    花了两天时间专门搞了一下git.整理一下分享给大家.以下我们開始.. . 转载请注明出处: http://blog.csdn.net/Hello_Chillax/article/details/474 ...

  7. 浏览器中beforeunload的使用

    打开一些慢的网站的时候只见浏览器在不停转圈,但是页面还停留在当前页面的,有些网站的效果是你点击链接要跳到另一个页面的时候,在当前页面弹出一个框提示“正在加载中....”, 用到了浏览器的beforeu ...

  8. lua笔记二 赋值语句

    赋值是改变一个变量的值和改变表域的最基本的方法. a = "hello" .. "world" t.n = t.n + 1 Lua可以对多个变量同时赋值,变量列 ...

  9. LPAD在Oracle中和 mssql以及在MySQL中的不同用法 以及调用存储过程方法

    Oracle: ,') from dual ,,') from dual 效果: Mssql: )+ltrim(tt.number_no) from Tabletest as tt 效果: MySQL ...

  10. Hash(MD5校验工具)

    本站提供md5校验工具下载.Hash(md5校验工具)是一款小巧好用的哈希计算器,Hash支持文件拖放,速度很快,可以计算文件的MD5.SHA1.CRC32 的值.在论坛上.软件发布时经常用Hash ...