1. 程式人生 > >飲冰三年-人工智慧-Python-20 Python執行緒

飲冰三年-人工智慧-Python-20 Python執行緒

程序:最小的資料單元

執行緒:最小的執行單元

一:

1:執行緒1

import threading #執行緒
import time
def Music():
    print("Listen Music Begin %s" %time.ctime())
    time.sleep(3)
    print("Listen Music End %s" %time.ctime())
def Game():
    print("Play Game Begin %s" %time.ctime())
    time.sleep(5)
    print("Play Game End %s
" %time.ctime()) if __name__ == '__main__': t1 = threading.Thread(target=Music) t1.start() t2 = threading.Thread(target=Game) t2.start() print("ending......")
例項1

import threading #執行緒
import time
def Music():
    print("Listen Music Begin %s" %time.ctime())
    time.sleep(
3) print("Listen Music End %s" %time.ctime()) def Game(): print("Play Game Begin %s" %time.ctime()) time.sleep(5) print("Play Game End %s" %time.ctime()) if __name__ == '__main__': t1 = threading.Thread(target=Music) t1.start() t2 = threading.Thread(target=Game) t2.start() t1.join() t2.join()
print("ending......")
執行緒join

import threading #執行緒
import time
def Music(musicName):
    print("Listen Music【%s】 Begin %s" %(musicName,time.ctime()))
    time.sleep(3)
    print("Listen Music【%s】 End %s" %(musicName,time.ctime()))
def Game(playName):
    print("Play Game【%s】 Begin %s" %(playName,time.ctime()))
    time.sleep(5)
    print("Play Game【%s】 End %s" %(playName,time.ctime()))
if __name__ == '__main__':
    t1 = threading.Thread(target=Music,args=("My Heart Will Go On",))
    t2 = threading.Thread(target=Game,args=("植物大戰殭屍",))
    threads=[]
    threads.append(t1)
    threads.append(t2)
    for t in threads:
        t.start();
    print("ending......")
執行緒引數

import threading #執行緒
import time
def Music(musicName):
    print("Listen Music【%s】 Begin %s" %(musicName,time.ctime()))
    time.sleep(3)
    print("Listen Music【%s】 End %s" %(musicName,time.ctime()))
def Game(playName):
    print("Play Game【%s】 Begin %s" %(playName,time.ctime()))
    time.sleep(5)
    print("Play Game【%s】 End %s" %(playName,time.ctime()))
t1 = threading.Thread(target=Music,args=("My Heart Will Go On",))
t2 = threading.Thread(target=Game,args=("植物大戰殭屍",))
threads=[]
threads.append(t1)
threads.append(t2)
if __name__ == '__main__':
    t2.setDaemon(True)
    for t in threads:
        t.start();
    print("ending......")
守護執行緒

# 多執行緒為什麼要使用鎖?
# 多執行緒共用一個數據集,不使用可能會造成資料不準確
#1:引入多執行緒模組
import threading
import time
# 2:定義一個變數(資料集)
num=100
# 3:定義一個方法,操作全域性變數
def sub():
    global num
    temp = num
    time.sleep(0.001)
    num=temp-1
# 4:建立多個執行緒
ts=[]
for i in range(0,100):
    t=threading.Thread(target=sub)
    t.start()
    ts.append(t)
for t in  ts:
    t.join()
print(num)
未加鎖,資料結果有誤

# 多執行緒為什麼要使用鎖?
# 多執行緒共用一個數據集,不使用可能會造成資料不準確
#1:引入多執行緒模組
import threading
import time
# 2:定義一個變數(資料集)
num=100
# 3:定義一個方法,操作全域性變數
# 5:建立鎖
lock=threading.Lock()
def sub():
    global num
    # 5.1 加鎖
    lock.acquire()
    temp = num
    time.sleep(0.001)
    num=temp-1
    # 5.2 釋放鎖
    lock.release()
# 4:建立多個執行緒
ts=[]
for i in range(0,100):
    t=threading.Thread(target=sub)
    t.start()
    ts.append(t)
for t in  ts:
    t.join()

print(num)
同步鎖