zoukankan      html  css  js  c++  java
  • threading python2 和python3

    from __future__ import division                                                 
    from __future__ import print_function                                           
                                                                                    
    import threading                                                                
                                                                                    
    balance = 0                                                                     
                                                                                    
    def change_it(n):                                                               
        global balance                                                              
        balance +=n                                                                 
        balance -=n                                                                 
                                                                                    
    def run_thread(n):                                                              
        for i in range(10000):                                                      
            change_it(n)                                                            
                                                                                    
    def create_thread():                                                            
        for i in range(30):                                                         
            t1 = threading.Thread(target=run_thread,args=(1,))                      
            t2 = threading.Thread(target=run_thread,args=(-1,))                     
            t1.start()                                                              
            t2.start()                                                              
            t1.join()                                                               
            t2.join()                                                               
            print(balance)                                                          
                                                                                    
    def _test_thread():                                                             
        create_thread()                                                             
                                                                                    
    def test():                                                                     
        _test_thread()                                                              
                                                                                    
    test()             
    

    输出

    Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
    #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
    ('64bit', 'ELF')
    Python:2.7.12 (default, Dec  4 2017, 14:50:18) 
    [GCC 5.4.0 20160609]
    6
    6
    -1
    0
    -28
    -41
    -40
    -49
    -56
    -54
    -60
    -63
    -68
    -73
    -69
    -78
    -53
    -60
    -53
    -58
    -48
    -71
    -82
    -83
    -130
    -129
    -111
    -100
    -84
    -173
    
    Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
    #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
    ('64bit', 'ELF')
    Python:3.5.2 (default, Nov 23 2017, 16:37:01) 
    [GCC 5.4.0 20160609]
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    

    加锁后

    from __future__ import division                                                 
    from __future__ import print_function                                           
                                                                                    
    import threading                                                                
                                                                                    
    balance = 0                                                                     
    lock = threading.Lock()                                                         
                                                                                    
    def change_it(n):                                                               
        global balance                                                              
        balance +=n                                                                 
        balance -=n                                                                 
                                                                                    
    def run_thread(n):                                                              
        for i in range(10000):                                                      
            lock.acquire()                                                          
            try:                                                                    
                change_it(n)                                                        
            finally:                                                                
                lock.release()                                                      
                                                                                    
    def create_thread():                                                            
        for i in range(30):                                                         
            t1 = threading.Thread(target=run_thread,args=(1,))                      
            t2 = threading.Thread(target=run_thread,args=(-1,))                     
            t1.start()                                                              
            t2.start()                                                              
            t1.join()                                                               
            t2.join()                                                               
            print(balance)                                                          
                                                                                    
    def _test_thread():                                                             
        create_thread()                                                             
                                                                                    
    def test():                                                                     
        _test_thread()                                                              
                                                                                    
    test()
    

    输出

    Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
    #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
    ('64bit', 'ELF')
    Python:2.7.12 (default, Dec  4 2017, 14:50:18) 
    [GCC 5.4.0 20160609]
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    
    Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
    #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
    ('64bit', 'ELF')
    Python:3.5.2 (default, Nov 23 2017, 16:37:01) 
    [GCC 5.4.0 20160609]
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    
  • 相关阅读:
    问题:弹窗还没点击确认就执行了跳转
    关于版本的问题
    timeUtil
    使用jframe编写一个base64加密解密工具
    JMeter 命令行(非GUI模式)详解(一)-分布式(远程)执行脚本及查看指定结果、日志
    jmeter分布式压测 java.io.FileNotFoundException: rmi_keystore.jks (系统找不到指定的文件。)
    mysql5.7日志时间与系统时间不一致
    mysql查看执行sql语句的记录日志
    Appium如何获取appPackage和appActivity
    关于测试设置
  • 原文地址:https://www.cnblogs.com/vercont/p/10210139.html
Copyright © 2011-2022 走看看