多執行緒初識

2021-09-11 07:51:07 字數 4711 閱讀 1312

'''

多執行緒cpu :

程式:**指令集合

程序:程式執行過程 ctrl alt delete

執行緒:指令執行最小單位

執行緒安全-------------------慢-----同步

非執行緒安全(執行緒不安全)---快-----非同步

繼承方式實現多執行緒

執行緒幾種狀態(生命週期)

建立狀態----->就緒狀態------->執行狀態--------->死亡狀態

\ \

\-----掛起狀態--\

執行緒鎖----互斥鎖 lock

''''''

import threading

def demo(name):

for i in range(10):

print(name,i)

t1 = threading.thread(target = demo,args = ("小雨",))

t2 = threading.thread(target = demo,args = ("小獲",))

t1.start()

t2.start()

''''''

import threading

import time

def run(name):

for i in range(1,101):

print(name,"跑了:",i,"公尺")

time.sleep(1)

p1 = threading.thread(target = run,args = ("小紅",))

p2 = threading.thread(target = run,args = ("小綠",))

p3 = threading.thread(target = run,args = ("小名",))

p1.start()

p2.start()

p3.start()

''''''

import threading

import time

list =

def cun(name):

while true:

print(name,"存了一袋速食麵,倉庫剩餘:",len(list))

time.sleep(1)

def qu(name):

while true:

if len(list) == 0:

time.sleep(1)

else:

list.pop()

print(name, "取了一袋速食麵,倉庫剩餘:", len(list))

time.sleep(1.5)

p1 = threading.thread(target = cun,args = ("p1",))

p2 = threading.thread(target = cun,args = ("p2",))

p3 = threading.thread(target = cun,args = ("p3",))

p4 = threading.thread(target = qu,args = ("p4",))

p5 = threading.thread(target = qu,args = ("p5",))

p6 = threading.thread(target = qu,args = ("p6",))

p7 = threading.thread(target = qu,args = ("p7",))

p1.start()

p2.start()

p3.start()

p4.start()

p5.start()

p6.start()

p7.start()

''''''

import threading

import time

list =

for i in range(100):

def qu(name):

while len(list) > 0:

time.sleep(0.1)

list.pop()

print(name,"取了乙份烤冷麵,倉庫剩餘",len(list))

p1 = threading.thread(target = qu,args = ("p1",))

p2 = threading.thread(target = qu,args = ("p2",))

p3 = threading.thread(target = qu,args = ("p3",))

p1.start()

p2.start()

p3.start()

''''''

import threading

class mythread(threading.thread):

def __init__(self):

threading.thread.__init__(self)

def run(self):

for i in range(100):

print(i)

t1 = mythread()

t2 = mythread()

t1.start()

t2.start()

''''''

import threading,time

lock = threading.lock()

list = ["烤冷麵"]

def get():

#獲取鎖

lock.acquire()

if len(list) > 0:

time.sleep(1)

list.pop()

print("取走")

#釋放鎖

lock.release()

t1 = threading.thread(target = get)

t2 = threading.thread(target = get)

t3 = threading.thread(target = get)

t1.start()

t2.start()

t3.start()

''''''

import threading,time

list =

lockq = threading.lock()

lockc = threading.lock()

class mythread(threading.thread):

def __init__(self,name):

threading.thread.__init__(self)

self._name = name

def run(self):

if self._name == "取貨人":

while true:

self.qu()

time.sleep(1)

else:

while true:

self.cun()

time.sleep(0.5)

def cun(self):

lockc.acquire()

print("存貨,剩餘:",len(list))

lockc.release()

def qu(self):

lockq.acquire()

if len(list) >0:

list.pop()

print("取貨,剩餘:", len(list))

lockq.release()

p1 = mythread("存貨人")

p2 = mythread("存貨人")

p3 = mythread("取貨人")

p4 = mythread("取貨人")

p5 = mythread("取貨人")

p1.start()

p2.start()

p3.start()

p4.start()

p5.start()

''''''

import threading,time

list =

def sc( name):

while true:

time.sleep(1)

print(name, "存了乙份烤冷麵,剩餘:", len(list))

def xfz( name):

while true:

if len(list) == 0:

time.sleep(1)

else:

list.pop()

print(name, "取了乙份烤冷麵,剩餘:", len(list))

p1 = threading.thread(target=sc, args=("p1",))

p2 = threading.thread(target=sc, args=("p2",))

p3 = threading.thread(target=xfz, args=("p3",))

p4 = threading.thread(target=xfz, args=("p4",))

p5 = threading.thread(target=xfz, args=("p5",))

p1.start()

p2.start()

p3.start()

p4.start()

p5.start()

'''

多執行緒初識

先了解什麼是應用程式,應用程式就是類似qq,360可執行的應用程式,在乙個應用程式中,都會有程序。什麼是程序,程序中有多個不同的執行路徑,多個執行緒的集合。程序是執行中的程式。什麼是執行緒,乙個執行順序,乙個執行流程,執行路徑。每個執行緒互不影響,因為都在自己獨立執行。總結 程序就是執行的程式,它是...

初識多執行緒問題

在多執行緒程式中,新建立的執行緒不能訪問ui執行緒建立的視窗控制項,如果需要訪問視窗中的控制項,有以下解決辦法 1.可以在視窗建構函式中將checkforillegalcrossthreadcalls設定為 false public form1 2.使用委託呼叫 宣告委託 private deleg...

java多執行緒初識4

程序的概念 程序 官方的語言是計算機中的程式,關於某資料集合上的一次執行活動。使系統進行資源分配和排程的基本單位單位,是作業系統的基本單位,是作業系統結構的基礎。在現在的執行緒設計的計算機結構中,程序是執行緒的容器,程式是指令資料及組織形式的描述,程序是程式的實體,但是總的來說 程序是執行緒的容器。...