2018-09-13多线程(day19)

2018-09-13  本文已影响0人  naruto711

1.多线程技术

2.子线程

3.threading模块

import threading
import datetime
import time
def download(file):
    print('开始下载'+file,datetime.datetime.now())
    # 让线程阻塞10秒
    time.sleep(10)
    print(file+'下载结束',datetime.datetime.now())

if __name__ == '__main__':
    print('主线程中的代码')
    print(threading.current_thread())
    # 1.在主线程中下载两个电影:花费20秒
    # download('终结者2')
    # download('火影忍者')

    #2.在两个子线程中去下载两个电影
    '''
    Thread(target,)
    target:需要在子线程中调用的函数的函数名
    args:函数的实参
    返回值:创建好的线程对象
    '''
    t1 = threading.Thread(target=download,args=('终结者2',))
    # 开始执行t1对应的线程中的任务
    t1.start()
    t2 = threading.Thread(target=download,args=('火影忍者',))
    # 想要在子线程中执行任务,必须通过线程对象调用start方法才行
    t2.start()

4.面向对象的多线程技术

from threading import Thread
import datetime
import time
from random import randint

# 继承可以继承自己写,也可以继承系统或者别人写的
class DownloadThread(Thread):
    def __init__(self,file):
        super().__init__()
        self.file = file

    def run(self):
        print(self.file + '开始下载:',datetime.datetime.now())
        time.sleep(randint(5,10))
        print(self.file + '下载结束:',datetime.datetime.now())
if __name__ == '__main__':
    print('='*10)
    t1 = DownloadThread('沉默的羔羊')
    t1.start()
    print('+'*10)

5.join方法

from threading import Thread
import time
from datetime import datetime
from random import randint
class DownloadThread(Thread):
    def __init__(self,file):
        super().__init__()
        self.flie = file

    def run(self):
        print(self.flie + '开始下载:',datetime.now())
        time.sleep(randint(5,15))
        print(self.flie + '下载结束:',datetime.now())

if __name__ == '__main__':

    t1 = DownloadThread('美丽人生')
    t2 = DownloadThread('怦然心动')

    start = time.time()

    time1 = t1.start()
    time2 = t2.start()
    # 系统t1和t2中的代码都结束后才执行下面的代码
    t1.join()    # 这句代码后面的代码在t1对应的线程结束后才执行
    t2.join()

    end = time.time()
    print(end-start)

6.线程间的数据共享

import time
from threading import Thread,Lock
class Account:
    '''账号类'''
    def __init__(self,balance):
        # 余额
        self.balance = balance
        # 创建锁对象
        self.lock = Lock()

    # 存钱的过程:读出原来的余额,确定钱的一系列操作,将原来的余额加上存的数额产生最新的余额在保存
    def save_money(self,amount):
        '''存钱'''
        print('开始存钱')
        # 加锁
        self.lock.acquire()
        # 获取原来的余额
        old_amount = self.balance
        # 模拟时间消耗
        time.sleep(5)
        # 修改余额
        self.balance = old_amount + amount
        print('存款成功,余额剩余%d'%self.balance)
        self.lock.release()

    def get_money(self,amount):
        '''取钱'''
        old_amount = self.balance
        if old_amount < amount:
            print('余额不足')
            return
        time.sleep(5)
        self.balance = old_amount - amount

    def show_balance(self):
        print('当前余额为:',self.balance)

if __name__ == '__main__':
    # 创建账号
    account = Account(1000000000)

    # account.save_money(1000)
    # account.get_money(1000000000)
    # # account.show_balance()
    '''
    当多个线程同时对一个数据进行操作的时候,可能会出现数据混乱的问题
    '''
    t1 = Thread(target=account.save_money,args=(2000,))
    t2 = Thread(target=account.save_money,args=(1000,))
    # t3 = Thread(target=account.get_money,args=(1000000000,))
    t1.start()
    t2.start()
    # t3.start()
上一篇下一篇

猜你喜欢

热点阅读