zoukankan      html  css  js  c++  java
  • Python3之threading模块

    import threading
    
    # Tips:一个ThreadLocal变量虽然是全局变量,
    # 但每个线程都只能读写自己线程的独立副本,互不干扰。
    # ThreadLocal解决了参数在一个线程中各个函数之间互相传递的问题。
    
    # 创建全局ThreadLocal对象
    local_school = threading.local()
    
    def process_student():
        # 获取当前线程关联的student:
        std = local_school.student
        print('Hello, %s (in %s)' % (std, threading.current_thread().name))
    
    def process_thread(name):
        # 绑定 TheadLocal的Student
        local_school.student = name
        # 调用上面的方法
        process_student()
    
    # 声明一个名字为:Thread-A的线程 执行process_thread方法 传入name为Memor
    # Tips: 此处传入的target的方法名称不需要加() 
    t1 = threading.Thread(target= process_thread, args=('Memor',),name= 'Thread-A')
    
    
    # 声明一个名字为:Thread-B的线程 执行process_thread方法 传入name为Godliness
    # Tips: 此处传入的target的方法名称不需要加() 
    t2 = threading.Thread(target= process_thread, args=('Godliness',),name= 'Thread-B')
    
    # 启动    
    t1.start()
    t2.start()
    t1.join()
    t2.join()
  • 相关阅读:
    codevs 1450 xth 的旅行
    Loj #6287 诗歌
    Codeforces 323C Two permutations
    Spoj MKTHNUM
    [TJOI2015]弦论
    Spoj SUBLEX
    bzoj 4338: BJOI2015 糖果
    bzoj 3462: DZY Loves Math II
    bzoj 2843: 极地旅行社
    清北学堂模拟赛d4t5 b
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10211015.html
Copyright © 2011-2022 走看看