import threading
from time import sleep
from datetime import datetime
# 1.主線程和子線程
'''
默認情況下,進程有一個線程纵柿,這個線程叫主線程蜈抓。(主線程是系統(tǒng)自動創(chuàng)建的)
默認程序所有的代碼是在主線程下執(zhí)行的
程序員通過代碼創(chuàng)建的線程都叫子線程资昧。
'''
# 2.創(chuàng)建子線程
'''
python內(nèi)置的threading模塊中提供了和線程操作相關(guān)的類和方法
threading模塊中有個Thread類撤缴,這個或者這個的子類的對象就是線程對象
需要子線程的時候就創(chuàng)建Thread類的對象
'''
def download(name):
print('%s: 開始下載:%s' % (datetime.now(), name))
print(threading.current_thread())
sleep(5)
print('%s: 下載完成:%s' % (datetime.now(), name))
if __name__ == '__main__':
# download('龍貓')
# 1.創(chuàng)建線程對象
'''
1)語法
Thread(target=函數(shù)虎眨, args=實參列表)
2)說明
target - 必須要賦一個函數(shù)凄敢,這個函數(shù)會在子線程中調(diào)用
args - 賦一個元組扑庞,元組中的元素就是調(diào)用target函數(shù)的時候需要傳的參數(shù)
'''
# 1.創(chuàng)建兩個子線程對象
t1 = threading.Thread(target=download, args=('龍貓',))
t2 = threading.Thread(target=download, args=('你的名字',))
# 2.讓子線程開始執(zhí)行
'''
線程對象.start() - 在子線程中去調(diào)用target對應(yīng)函數(shù)栅隐,并將args中的值作為實參
'''
t1.start()
t2.start()
# 3.程序的結(jié)束(進程的結(jié)束)
'''
一個進程的結(jié)束佣赖,看是否這個進程中的每個線程都結(jié)束。
線程結(jié)束:任務(wù)執(zhí)行完成萎胰,遇到exit()函數(shù)榔组,發(fā)生異常
程序出現(xiàn)異常只會影響當前出現(xiàn)異常的線程检痰,其他線程正常運行
exit()只結(jié)束當前的線程
'''
1.通過創(chuàng)建Thread類的子類對象來創(chuàng)建子線程
1)聲明一個類繼承Thread
class DownloadThread(threading.Thread):
def __init__(self, name):
super().__init__()
self.name = name
# 2)實現(xiàn)run方法椎椰,這個方法中代碼就是會在子線程中執(zhí)行的代碼
def run(self):
print('%s - 開始下載:%s ' % (datetime.now(), self.name))
sleep(4)
# print(threading.current_thread())
print('%s - 下載完成:%s' % (datetime.now(), self.name))
if __name__ == '__main__':
# 3)創(chuàng)建子類的線程對象
t1 = DownloadThread('龍貓',)
# 4)通過start去執(zhí)行子線程中的任務(wù)
t1.start()
import threading
import datetime
import time
import random
class Download(threading.Thread):
def __init__(self, name):
super().__init__()
self.name = name
def run(self):
print('%s - 開始下載%s' % (datetime.datetime.now(), self.name))
t = random.randint(2, 6)
time.sleep(t)
print('%s - %s下載完成' % (datetime.datetime.now(), self.name))
if __name__ == '__main__':
t1 = Download('龍貓')
t2 = Download('你的名字')
t3 = Download('女教師')
t1.start()
t2.start()
# 1.join的用法
'''
線程對象.join()
其他的代碼
其他代碼會等到指定的線程對象結(jié)束后才會執(zhí)行
'''
t1.join()
t2.join()
t3.start()
print('下載完成')
import threading
import time
# 1.怎么加鎖
'''
1)保證一個數(shù)據(jù)對應(yīng)一把鎖:創(chuàng)建鎖對象
2)使用數(shù)據(jù)前加鎖:鎖對象.acquire()
3)數(shù)據(jù)使用完成后釋放鎖
'''
account = 1000
# 1.創(chuàng)建鎖對象
lock = threading.Lock()
def save_money(num):
# 2.加鎖
lock.acquire()
print('開始存錢')
global account
balance = account
time.sleep(4)
account = balance + num
# 3.釋放鎖
lock.release()
print('存完后余額:%d' % account)
def draw_money(num):
lock.acquire()
print('開始取錢')
global account
balance = account
if balance < num:
print('余額不足...')
return
time.sleep(4)
account = balance - num
lock.release()
print('取完后余額:%d' % account)
t1 = threading.Thread(target=save_money, args=(1000,))
t2 = threading.Thread(target=draw_money, args=(500,))
t1.start()
t2.start()
import threading
import time
class Account:
def __init__(self, name, id_card, tel, balance):
self.balance = balance
self.name = name
self.id_card = id_card
self.card_number = '2221'
self.tel = tel
self.lock = threading.Lock()
def save_money(self, num):
# lock.acquire()
print('開始存錢')
self.lock.acquire()
balance = self.balance
time.sleep(4)
self.balance = balance + num
# 3.釋放鎖
# lock.release()
print('存完后余額:%d' % self.balance)
self.lock.release()
def draw_money(self, num):
# lock.acquire()
print('開始取錢')
self.lock.acquire()
balance = self.balance
if balance < num:
print('余額不足')
time.sleep(4)
self.balance = balance - num
# 3.釋放鎖
# lock.release()
print('取完后余額:%d' % self.balance)
self.lock.release()
account = Account('zzq', '231', '213', 100)
account2 = ('21e', '421', '213', 100)
t1 = threading.Thread(target=account.save_money, args=(100,))
t2 = threading.Thread(target=account.draw_money, args=(50,))
t1.start()
t2.start()