用wait notify實現生產者和消費者程式

2021-10-17 08:54:49 字數 1069 閱讀 6095

public

class 用waitnotify實現生產者消費者問題

bulletsnum ++

; system.out.

println

("彈匣未滿,開始裝彈,現有子彈:"

+ bulletsnum)

;notifyall()

;}catch

(interruptedexception e)}/*

* 射擊方法

*/synchronized

void

shoot()

system.out.

println

("彈匣有彈,開始射擊,現有子彈:"

+ bulletsnum)

; bulletsnum --

; system.out.

println

("射擊完還剩子彈:"

+ bulletsnum)

;notifyall()

;}catch

(interruptedexception e)}}

/* * 生產者執行緒 往槍膛中壓入子彈

* */

private

static

class

addbullets

implements

runnable

@override

public

void

run()}

}/** 消費者程序 射擊

* */

private

static

class

shoot

implements

runnable

@override

public

void

run()}

}public

static

void

main

(string[

] args)

}

wait notify 實現多執行緒交叉備份

建立20個執行緒,其中10個執行緒是將資料備份到 a 資料庫中,另外10 個執行緒將資料備份到 b 資料庫中,並且備份 a 資料庫和 備份 b 資料庫的是交叉執行的。1 實現備份 a 資料庫和備份 b 資料庫的 task。description 當flag true的時候備份 a 資料庫 當flag...

wait notify 實現多執行緒交叉備份

建立20個執行緒,其中10個執行緒是將資料備份到 a 資料庫中,另外10 個執行緒將資料備份到 b 資料庫中,並且備份 a 資料庫和 備份 b 資料庫的是交叉執行的。1 實現備份 a 資料庫和備份 b 資料庫的 task。description 當flag true的時候備份 a 資料庫 當flag...

用Python多執行緒實現生產者消費者模式

在軟體開發的過程中,經常碰到這樣的場景 某些模組負責生產資料,這些資料由其他模組來負責處理 此處的模組可能是 函式 執行緒 程序等 產生資料的模組稱為生產者,而處理資料的模組稱為消費者。在生產者與消費者之間的緩衝區稱之為倉庫。生產者負責往倉庫運輸商品,而消費者負責從倉庫裡取出商品,這就構成了生產者消...