不断的练,加深记忆吧。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading
import time

exitFlag = 0

def first_function():
    print (threading.currentThread().getName() + \
           str(' is Starting \n'))
    time.sleep(2)
    print (threading.currentThread().getName() + \
           str(' is Exiting \n'))

def second_function():
    print (threading.currentThread().getName() + \
           str(' is Starting \n'))
    time.sleep(2)
    print (threading.currentThread().getName() + \
           str(' is Exiting \n'))

def third_function():
    print (threading.currentThread().getName() + \
           str(' is Starting \n'))
    time.sleep(2)
    print (threading.currentThread().getName() + \
           str(' is Exiting \n'))

class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print("Starting " + self.name)
        print_time(self.name, self.counter, 5)
        print("Exiting " + self.name)

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            thread.exit()
        time.sleep(delay)
        print("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1

if __name__ == "__main__":
    t1 = threading.Thread\
         (name='first_function', target=first_function)
    t2 = threading.Thread\
         (name='second_function', target=second_function)
    t3 = threading.Thread\
         (name='third_function', target=third_function)

    t1.start()
    t2.start()
    t3.start()
    t1.join()
    t2.join()
    t3.join()

    thread1 = myThread(1, "Thread-1", 1)
    thread2 = myThread(2, "Thread-2", 2)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    print("Exiting Main Thread")

基于类和基于函数的python多线程样例的更多相关文章

  1. Atitit.prototype-base class-based  基于“类” vs 基于“原型”

    Atitit.prototype-base class-based  基于“类” vs 基于“原型” 1. 基于“类” vs 基于“原型”1 2.  对象的产生有两种基本方式.一种是以原型(proto ...

  2. gtk+3.0的环境配置及基于gtk+3.0的python简单样例

    /*********************************************************************  * Author  : Samson  * Date   ...

  3. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  4. java多线程样例

    这里我们做一个完整的样例来说明线程产生的方式不同而生成的线程的差别: package debug; import java.io.*;import java.lang.Thread; class My ...

  5. Python word_cloud 样例 标签云系列(三)

    转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...

  6. 函数指针使用演示样例(參考Linux-内核代码)

    本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...

  7. 维特比算法(Viterbi)及python实现样例

    维特比算法(Viterbi) 维特比算法 维特比算法shiyizhong 动态规划算法用于最可能产生观测时间序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔科夫模型中.术语“维特 ...

  8. Boost Python官方样例(三)

    导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...

  9. Boost Python官方样例(一)

    配置环境 $ cat /etc/os-release NAME="Ubuntu" VERSION="16.04 LTS (Xenial Xerus)" ID=u ...

随机推荐

  1. POJ 3322(广搜)

    ---恢复内容开始--- http://poj.org/problem?id=3322 题意:http://jandan.net/2008/01/24/bloxorz.html就是这个鬼游戏 我也是郁 ...

  2. Failed to issue method call: Unit httpd.service failed to load: No such file or directory.

    centos7修改httpd.service后运行systemctl restart httpd.service提示 Failed to issue method call: Unit httpd.s ...

  3. 4.nodejs权威指南--TCP和UDP

    1. TCP和UDP 1.1 TCP服务端 var net = require('net'); var server = net.createServer(); server.on('connecti ...

  4. OGNL表达式

    一.什么是OGNL,有什么特点?     OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言 ...

  5. java 入门 第二季4

    1. 多态 继承是多态的实现基础 引用的多态 父类的引用可以指向本类的对象 父类的引用可以指向子类的对象 方法的多态 创建本类对象时,调用本类方法 2种是调用子类的方法或继承的方法 子类中添加独有的方 ...

  6. Python 输入输出

    语法注释 输入输出 #语法缩进,4个空格 #注释 #冒号:结尾,缩进的预计视为代码块 #大小写敏感 #输出 print 300 print 'hello','world' #输入 a=raw_inpu ...

  7. [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别

    原文链接:http://blog.csdn.net/zhaokaiqiang1992 FragmentPagerAdapter是android-support-v4支持包里面出现的一个新的适配器,继承 ...

  8. hiho一下第一周 最长回文子串

    题目链接:http://hihocoder.com/contest/hiho1/problem/1 #include <iostream> #include <cstdio> ...

  9. codeforces 519C. A and B and Team Training 解题报告

    题目链接:http://codeforces.com/contest/519/problem/C 题目意思:给出 n 个  experienced participants  和 m 个 newbie ...

  10. 【轮子】发现一个效果丰富酷炫的Android动画库

    没有什么比发现一个好轮子更让人开心的了. 这个库分分钟提高交互体验 :AndroidViewAnimations 一张图说明一切 配置和使用也相当简单 GitHub地址