• 使用的是 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. com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024)

    ### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024). You ...

  2. echarts 认知笔记

    0.echarts.setOption的核心认知 请注意,它是合并对象,而不是替换对象. 举个简单的例子,如果你第一次setOption时在series中配置了10个对象. 那么你下一次你意图使用只有 ...

  3. java打印条形码Code128C

    生成编码类型为Code128C的条形码的javaCODE: package test; import java.awt.Color; import java.awt.Graphics; import ...

  4. FIR基本型仿真_03

    作者:桂. 时间:2018-02-05 20:50:54 链接:http://www.cnblogs.com/xingshansi/p/8419452.html 一.仿真思路 设计低通滤波器(5阶,6 ...

  5. JDK1.5新特性,基础类库篇,线程类(Thread)增强了哪些

    java.lang.Thread类增强特性如下: 线程优先级已经更改.java.lang.Thread.MIN_PRIORITY = 1 java.lang.Thread.NORM_PRIORITY ...

  6. PHP文件锁定写入实例分享

    PHP文件锁定写入实例解析. 原文地址:http://www.jbxue.com/article/23118.html PHP文件写入方法,以应对多线程写入,具体代码: function file_w ...

  7. schema的作用

    1,如果schema中定义的字段类型和数据库中该字段存储值的类型不一致(可以不定义,但定义的时候类型必须一致),则该字段查找不到,mongoose不会返回该字段的数据 2,如果数据库中有字段a,而sc ...

  8. mysql protocol

    http://hutaow.com/blog/2013/11/06/mysql-protocol-analysis/ https://dev.mysql.com/doc/internals/en/cl ...

  9. .NET 获得指定XML配置文件内容

    /// <summary> /// 获得指定XML文件内容 /// </summary> /// <param name="strPath">X ...

  10. linux怎么关闭iptables linux如何关闭防火墙

    Linux系统下面自带了防火墙iptables,iptables可以设置很多安全规则.但是如果配置错误很容易导致各种网络问题,那么如果要关闭禁用防火墙怎么操作呢,咗嚛本经验以centos系统为例演示如 ...