基础认识多线程并发,资源抢占,资源加锁

前尘篇:pt1首先不加锁看看资源会怎么样
# coding=utf-8
import threading
import time
k =1

def walk(n):
           global k
           k = k+n
           k=  k-n
           print "我是悟空||菩萨在此"
def run(n):
        for j in range(1,9):
            walk(n)
if __name__ == "__main__":

        t1  = threading.Thread(target=run,name='thread_mo1',args=(5,))
        t2 = threading.Thread(target=run,name="thread_no2",args=(4,))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        print k
结果如下:
我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此我是悟空||菩萨在此

我是悟空||菩萨在此我是悟空||菩萨在此

我是悟空||菩萨在此我是悟空||菩萨在此

我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此
我是悟空||菩萨在此
1
出现三组 两个线程同时抢夺资源情况,线程打起来了

那么问题来了,这样下去服务器的炸了怎么解决


往事篇:pt2加锁线程,并发时保护资源

# coding=utf-8
import threading
import time

"""
Python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁。
threading模块提供的其他方法:
        threading.currentThread(): 返回当前的线程变量。
        threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
        threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果
"""
"""
Thread类提供了以下方法:
    run(): 用以表示线程活动的方法。
    start():启动线程活动。
    join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
    isAlive(): 返回线程是否活动的。
    getName(): 返回线程名。
    setName(): 设置线程名。
"""
# 生成线程锁,要用过来取
mutex = threading.Lock()
class TestTreading(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    """重写父类thread的run()方法,此方法一旦生成线程对象自动调用"""
    def run(self):
        """开始上锁"""
        mutexFlage = mutex.acquire()
        """判断上锁ok,你们一个一个来,放心去和菩萨说吧,在说的时候另外一个猴子不会吵你说话了"""
        if mutexFlage:
            for i in range(5):
                print "菩萨我是真悟空{}......--{}|菩萨坐在此地|".format(self.getName(),time.asctime())
                print threading.current_thread,threading.activeCount()
        """事情完了解除锁让别的线程用,哪个线程需要资源可以用了,本线程用完了,不然就blocked阻塞,形成死锁了"""
        mutex.release()
if __name__ =="__main__":
    for i in range(10):
        t = TestTreading()
        t.start()
结果如下问题解决了并发资源同时被抢夺:
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/py3project/ThreadModule/basic_thread.py
菩萨我是真悟空Thread-1......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-1......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-1......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-1......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-1......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-2......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-2......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-2......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 5
菩萨我是真悟空Thread-2......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-2......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-3......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-3......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 7
菩萨我是真悟空Thread-3......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-3......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-3......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-4......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-4......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-4......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-4......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-4......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 8
菩萨我是真悟空Thread-5......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 7
菩萨我是真悟空Thread-5......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 7
菩萨我是真悟空Thread-5......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 7
菩萨我是真悟空Thread-5......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 7
菩萨我是真悟空Thread-5......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 7
菩萨我是真悟空Thread-6......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-6......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-6......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-6......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-6......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 6
菩萨我是真悟空Thread-7......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 5
菩萨我是真悟空Thread-7......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 5
菩萨我是真悟空Thread-7......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 5
菩萨我是真悟空Thread-7......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 5
菩萨我是真悟空Thread-7......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 5
菩萨我是真悟空Thread-8......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-8......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-8......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-8......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-8......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 4
菩萨我是真悟空Thread-9......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-9......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-9......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-9......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-9......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 3
菩萨我是真悟空Thread-10......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 2
菩萨我是真悟空Thread-10......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 2
菩萨我是真悟空Thread-10......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 2
菩萨我是真悟空Thread-10......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 2
菩萨我是真悟空Thread-10......--Sun Nov 25 23:10:22 2018|菩萨坐在此地|
<function currentThread at 0x0000000002813B38> 2









 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值