當前位置:首頁 » 編程語言 » java生產者消費者

java生產者消費者

發布時間: 2022-06-29 01:22:08

1. java中生產者和消費者問題

好不好,要看情況,單這樣,提不提都問題不大。

類的架構,由設計者決定。

this 是指當前類的當前實例

2. java多生產者和多消費者

public static void main(String[] args) {
Buffer buffer=new Buffer(); //創建一個臨界區對象
new Procer(buffer,100).start(); //創建一個生產者對象,並啟動其線程
new Consumer(buffer,200).start(); //創建一個消費者對象,並啟動其線程
new Consumer(buffer,201).start(); //創建第二個消費者對象,並啟動其線程
}

這一段代碼,多加入
new Consumer(buffer,201).start(); //創建第二個消費者對象,並啟動其線程
多加一段代碼創建一個消費者
多加入
new Procer(buffer,100).start();
創建一個生產者。
想要很多生產者和消費者?加就是了啊。

第四個文件 Buffer.java
這個是實現同步的主要緩存類。想要實現同步
在每個方法的聲明前都加入synchronized 就行
synchronized 線程鎖,很好用的。源代碼已經加入了
就比如
public synchronized void put(int value) { //同步方法控制臨界區內容寫入

3. java實現生產者和消費者問題的幾種方式

生產者消費者問題是多線程的一個經典問題,它描述是有一塊緩沖區作為倉庫,生產者可以將產品放入倉庫,消費者則可以從倉庫中取走產品。
解決生產者/消費者問題的方法可分為兩類:
採用某種機制保護生產者和消費者之間的同步;
在生產者和消費者之間建立一個管道。
第一種方式有較高的效率,並且易於實現,代碼的可控制性較好,屬於常用的模式。第二種管道緩沖區不易控制,被傳輸數據對象不易於封裝等,實用性不強。
在Java中有四種方法支持同步,其中前三個是同步方法,一個是管道方法。
wait()
/
notify()方法
await()
/
signal()方法
BlockingQueue阻塞隊列方法
PipedInputStream
/
PipedOutputStream
通過
wait()
/
notify()方法實現:
wait()
/
nofity()方法是基類Object的兩個方法:
wait()方法:當緩沖區已滿/空時,生產者/消費者線程停止自己的執行,放棄鎖,使自己處於等等狀態,讓其他線程執行。
notify()方法:當生產者/消費者向緩沖區放入/取出一個產品時,向其他等待的線程發出可執行的通知,同時放棄鎖,使自己處於等待狀態。
通過await()
/
signal()方法實現:
await()和signal()的功能基本上和wait()
/
nofity()相同,完全可以取代它們,但是它們和新引入的鎖定機制Lock直接掛鉤,具有更大的靈活性。通過在Lock對象上調用newCondition()方法,將條件變數和一個鎖對象進行綁定,進而控制並發程序訪問競爭資源的安全。
通過BlockingQueue方法實現:
它是一個已經在內部實現了同步的隊列,實現方式採用的是我們第2種await()
/
signal()方法。它可以在生成對象時指定容量大小。它用於阻塞操作的是put()和take()方法:
put()方法:類似於我們上面的生產者線程,容量達到最大時,自動阻塞。
take()方法:類似於我們上面的消費者線程,容量為0時,自動阻塞。

4. java 生產者/消費者問題

/**
* 公共資源類
*/
public class PublicResource {
private int number = 0;
/**
* 增加公共資源
*/
public synchronized void increace() {
while (number != 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++;
System.out.println(number);
notify();
}
/**
* 減少公共資源
*/
public synchronized void decreace() {
while (number == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number--;
System.out.println(number);
notify();
}
}

/**

* 生產者線程,負責生產公共資源
*/
public class ProcerThread implements Runnable {
private PublicResource resource;
public ProcerThread(PublicResource resource) {
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
resource.increace();
}
}
}
/**
* 消費者線程,負責消費公共資源
*/
public class ConsumerThread implements Runnable {
private PublicResource resource;
public ConsumerThread(PublicResource resource) {
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
resource.decreace();
}
}
}

public class ProcerConsumerTest {
public static void main(String[] args) {
PublicResource resource = new PublicResource();
new Thread(new ProcerThread(resource)).start();
new Thread(new ConsumerThread(resource)).start();
new Thread(new ProcerThread(resource)).start();
new Thread(new ConsumerThread(resource)).start();
new Thread(new ProcerThread(resource)).start();
new Thread(new ConsumerThread(resource)).start();
}
}

5. Java線程生產者消費者問題

你的消費者線程先啟動的,因為index==0,所以進入了等待模式,而生產者線程沒有在push了之後進行喚醒,導致了問題。
測試在push方法中的index++後面添加notifyAll()方法可以解決該問題:
index++;
this.notifyAll();
Thread-1生產了1號饅頭
Thread-0消費了第1個饅頭
Thread-1生產了2號饅頭
Thread-0消費了第2個饅頭
Thread-1生產了3號饅頭
Thread-0消費了第3個饅頭
Thread-1生產了4號饅頭
Thread-0消費了第4個饅頭
Thread-1生產了5號饅頭
Thread-0消費了第5個饅頭
Thread-1生產了6號饅頭
Thread-0消費了第6個饅頭
Thread-1生產了7號饅頭
Thread-0消費了第7個饅頭
Thread-1生產了8號饅頭
Thread-0消費了第8個饅頭
Thread-1生產了9號饅頭
Thread-0消費了第9個饅頭

6. Java線程生產者與消費者

改成這樣就可以了

class WoTou {
int id;
WoTou(int id) {
this.id=id;
}
public String toString() {
return "WoTou"+id;
}
}

class SynStack {
WoTou[] ss = new WoTou[6];
int index;
SynStack(int index) {
this.index=index;
}
public synchronized void push(WoTou wt) {
while(index==ss.length) {
try {
this.wait();
}catch (InterruptedException e) {
e.printStackTrace();
}

}
this.notify();
ss[index]=wt;
index++;

}
public synchronized WoTou pop() {
while(index==0) {
try {
this.wait();
}catch(InterruptedException e) {
e.printStackTrace();
}

}
this.notify();
index--;
return ss[index];

}
}

class Procer implements Runnable{
SynStack syn;
Procer(SynStack syn) {
this.syn=syn;
}
public void proceWoTou() {
for(int i=0;i<20;i++) {
WoTou wt = new WoTou(i);
syn.push(wt);
System.out.println("生產了一個"+wt);
try {
Thread.sleep(200);
} catch(InterruptedException e) {
e.printStackTrace();
}
}

}
public void run() {
this.proceWoTou();

}
}

class Consumer implements Runnable{
SynStack syn;
Consumer(SynStack syn) {
this.syn=syn;
}
public void consumerWoTou() {
for(int i=0;i<20;i++) {
WoTou wt=syn.pop();
System.out.println("消費了一個"+wt);
try {
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}
}

}
public void run() {
this.consumerWoTou();

}
}

public class ProcerConsumer {
public static void main(String[] args) {
SynStack syn = new SynStack(0);
Procer p = new Procer(syn);
Consumer c = new Consumer(syn);
new Thread(p).start();
new Thread(c).start();
}
}

7. java 生產者與消費者 的簡單問題

1 toString是在控制台列印對象的時候會調用的對象的方法。。比如你定義個Sx對象sx。。然後System.out.println(sx);就會先調用sx的toString方法。。將得到的String對象列印在控制台。。每個類都繼承自Object。。Object類裡面有個toString方法。。返回的是該對象在內存中的地址。。如果你不重寫這個方法。。列印出來的東西你看不明白的。。
2 java中數據類型有基本類型(int那些)和引用類型(就是所謂的對象)。。java中數組是對象。。每個數組對象都有一個length屬性。。值是這個數組的元素個數。。對象調用屬性是不需要括弧的。。方法才需要。。
3 kuang ss=null;就是定義一個kuang對象ss。。其初始值為null。。為null的對象在內存中什麼都沒有
int index=0; Wotou[] tou=new Wotou[6];這兩個都不是為空的。。都有具體的初始值。。
4 Wotou wt=ss.pop();表示定義一個Wotou對象wt。。並將調用ss對象的pop方法的返回值賦值給wt對象。。具體你看下kuang 類的pop方法就知道了。。

8. JAVA模擬生產者與消費者實例

使用的生產者和消費者模型具有如下特點:
(1)本實驗的多個緩沖區不是環形循環的,也不要求按順序訪問。生產者可以把產品放到目前某一個空緩沖區中。
(2)消費者只消費指定生產者的產品。
(3)在測試用例文件中指定了所有的生產和消費的需求,只有當共享緩沖區的數據滿足了所有關於它的消費需求後,此共享緩沖區才可以作為空閑空間允許新的生產者使用。
(4)本實驗在為生產者分配緩沖區時各生產者間必須互斥,此後各個生產者的具體生產活動可以並發。而消費者之間只有在對同一產品進行消費時才需要互斥,同時它們在消費過程結束時需要判斷該消費對象是否已經消費完畢並清除該產品。
Windows
用來實現同步和互斥的實體。在Windows
中,常見的同步對象有:信號量(Semaphore)、
互斥量(Mutex)、臨界段(CriticalSection)和事件(Event)等。本程序中用到了前三個。使用這些對象都分
為三個步驟,一是創建或者初始化:接著請求該同步對象,隨即進入臨界區,這一步對應於互斥量的
上鎖;最後釋放該同步對象,這對應於互斥量的解鎖。這些同步對象在一個線程中創建,在其他線程
中都可以使用,從而實現同步互斥。當然,在進程間使用這些同步對象實現同步的方法是類似的。
1.用鎖操作原語實現互斥
為解決進程互斥進人臨界區的問題,可為每類臨界區設置一把鎖,該鎖有打開和關閉兩種狀態,進程執行臨界區程序的操作按下列步驟進行:
①關鎖。先檢查鎖的狀態,如為關閉狀態,則等待其打開;如已打開了,則將其關閉,繼續執行步驟②的操作。
②執行臨界區程序。
③開鎖。將鎖打開,退出臨界區。
2.信號量及WAIT,SIGNAL操作原語
信號量的初值可以由系統根據資源情況和使用需要來確定。在初始條件下信號量的指針項可以置為0,表示隊列為空。信號量在使用過程中它的值是可變的,但只能由WAIT,SIGNAL操作來改變。設信號量為S,對S的WAIT操作記為WAIT(S),對它的SIGNAL操作記為SIGNAL(S)。
WAIT(S):順序執行以下兩個動作:
①信號量的值減1,即S=S-1;
②如果S≥0,則該進程繼續執行;
如果
S(0,則把該進程的狀態置為阻塞態,把相應的WAITCB連人該信號量隊列的末尾,並放棄處理機,進行等待(直至其它進程在S上執行SIGNAL操作,把它釋放出來為止)。
SIGNAL(S):順序執行以下兩個動作
①S值加
1,即
S=S+1;
②如果S)0,則該進程繼續運行;
如果S(0則釋放信號量隊列上的第一個PCB(既信號量指針項所指向的PCB)所對應的進程(把阻塞態改為就緒態),執行SIGNAL操作的進程繼續運行。
在具體實現時注意,WAIT,SIGNAL操作都應作為一個整體實施,不允許分割或相互穿插執行。也就是說,WAIT,SIGNAL操作各自都好像對應一條指令,需要不間斷地做下去,否則會造成混亂。
從物理概念上講,信號量S)時,S值表示可用資源的數量。執行一次WAIT操作意味著請求分配一個單位資源,因此S值減1;當S<0時,表示已無可用資源,請求者必須等待別的進程釋放了該類資源,它才能運行下去。所以它要排隊。而執行一次SIGNAL操作意味著釋放一個單位資源,因此S值加1;若S(0時,表示有某些進程正在等待該資源,因而要把隊列頭上的進程喚醒,釋放資源的進程總是可以運行下去的。
---------------
/**
*
生產者
*
*/
public
class
Procer
implements
Runnable{
private
Semaphore
mutex,full,empty;
private
Buffer
buf;
String
name;
public
Procer(String
name,Semaphore
mutex,Semaphore
full,Semaphore
empty,Buffer
buf){
this.mutex
=
mutex;
this.full
=
full;
this.empty
=
empty;
this.buf
=
buf;
this.name
=
name;
}
public
void
run(){
while(true){
empty.p();
mutex.p();
System.out.println(name+"
inserts
a
new
proct
into
"+buf.nextEmptyIndex);
buf.nextEmptyIndex
=
(buf.nextEmptyIndex+1)%buf.size;
mutex.v();
full.v();
try
{
Thread.sleep(1000);
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
}
}
}
---------------
/**
*
消費者
*
*/
public
class
Customer
implements
Runnable{
private
Semaphore
mutex,full,empty;
private
Buffer
buf;
String
name;
public
Customer(String
name,Semaphore
mutex,Semaphore
full,Semaphore
empty,Buffer
buf){
this.mutex
=
mutex;
this.full
=
full;
this.empty
=
empty;
this.buf
=
buf;
this.name
=
name;
}
public
void
run(){
while(true){
full.p();
mutex.p();
System.out.println(name+"
gets
a
proct
from
"+buf.nextFullIndex);
buf.nextFullIndex
=
(buf.nextFullIndex+1)%buf.size;
mutex.v();
empty.v();
try
{
Thread.sleep(1000);
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
}
}
}
-------------------------
/**
*
緩沖區
*
*/
public
class
Buffer{
public
Buffer(int
size,int
nextEmpty,int
nextFull){
this.nextEmptyIndex
=
nextEmpty;
this.nextFullIndex
=
nextFull;
this.size
=
size;
}
public
int
size;
public
int
nextEmptyIndex;
public
int
nextFullIndex;
}
-----------------
/**
*
此類用來模擬信號量
*
*/
public
class
Semaphore{
private
int
semValue;
public
Semaphore(int
semValue){
this.semValue
=
semValue;
}
public
synchronized
void
p(){
semValue--;
if(semValue<0){
try
{
this.wait();
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
}
}
public
synchronized
void
v(){
semValue++;
if(semValue<=0){
this.notify();
}
}
}
------------------------
public
class
Test
extends
Thread
{
public
static
void
main(String[]
args)
{
Buffer
bf=new
Buffer(10,0,0);
Semaphore
mutex=new
Semaphore(1);
Semaphore
full=new
Semaphore(0);
Semaphore
empty=new
Semaphore(10);
//new
Thread(new
Procer("p001",mutex,full,empty,bf)).start();
Procer
p=new
Procer("p001",mutex,full,empty,bf);
new
Thread(new
Procer("p002",mutex,full,empty,bf)).start();
new
Thread(new
Procer("p003",mutex,full,empty,bf)).start();
new
Thread(new
Procer("p004",mutex,full,empty,bf)).start();
new
Thread(new
Procer("p005",mutex,full,empty,bf)).start();
try{
sleep(3000);
}
catch(Exception
ex)
{
ex.printStackTrace();
}
new
Thread(new
Customer("c001",mutex,full,empty,bf)).start();
new
Thread(new
Customer("c002",mutex,full,empty,bf)).start();
new
Thread(new
Customer("c003",mutex,full,empty,bf)).start();
new
Thread(new
Customer("c004",mutex,full,empty,bf)).start();
new
Thread(new
Customer("c005",mutex,full,empty,bf)).start();
}
}
--------------------------------------------

9. java 多線程 生產者-消費者問題

為空、是否已滿,容器沒有自定義的話,就要在線程類中每次生產之前判斷容器是否已滿(這個已經由容器判斷),在消費時要判斷容器是否為空

2、一般,線程同步最好用synchronized關鍵字鎖定同步代碼,然後通過wait()和notify()方法實現線程同步,不過容器容量大一點才能看到效果。代碼如下:
class BQProc implements Runnable {
private BlockingQueue<Integer> queue;

public BQProc(BlockingQueue<Integer> queue) {
this.queue = queue;
}

public void run() {
synchronized(queue) {
for (int proct = 1; proct <= 10; proct++) {

try {
if(queue.size() == 5) {
queue.wait();
}
queue.put(proct);// 放進去一個
System.out.println("放在 --> " + this.queue.size());
queue.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

class BQConsume implements Runnable {
private BlockingQueue<Integer> queue;

public BQConsume(BlockingQueue<Integer> queue) {
this.queue = queue;
}

public void run() {
synchronized(queue) {
for (int i = 1; i <= 10; i++) {
try {
if(queue.isEmpty()) {
queue.wait();
}
System.out.println("拿出 <-- " + this.queue.size());
queue.take();// 拿出來一個
queue.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
運行結果:
放在 --> 1
放在 --> 2
放在 --> 3
放在 --> 4
放在 --> 5
拿出 <-- 5
拿出 <-- 4
拿出 <-- 3
拿出 <-- 2
拿出 <-- 1
放在 --> 1
放在 --> 2
放在 --> 3
放在 --> 4
放在 --> 5
拿出 <-- 5
拿出 <-- 4
拿出 <-- 3
拿出 <-- 2
拿出 <-- 1

PS:當基數大到一定程度才可以看到想要的結果
3、如果一定要用Thread.sleep()實現的話可以用循環控制,代碼如下:

class BQProc implements Runnable {
private BlockingQueue<Integer> queue;

public BQProc(BlockingQueue<Integer> queue) {
this.queue = queue;
}

public void run() {
for (int proct = 1; proct <= 10; proct++) {

try {
while(queue.size() == 5) {
Thread.sleep(1000);
}
Thread.sleep((int) (Math.random() * 1000));
queue.put(proct);// 放進去一個
System.out.println("放在 --> " + this.queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class BQConsume implements Runnable {
private BlockingQueue<Integer> queue;

public BQConsume(BlockingQueue<Integer> queue) {
this.queue = queue;
}

public void run() {
for (int i = 1; i <= 10; i++) {
try {
while(queue.isEmpty()) {
Thread.sleep(1000);
}
Thread.sleep((int) (Math.random() * 1000));
System.out.println("拿出 <-- " + this.queue.size());
queue.take();// 拿出來一個
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
執行結果:
放在 --> 1
放在 --> 2
拿出 <-- 2
拿出 <-- 1
放在 --> 1
拿出 <-- 1
放在 --> 1
放在 --> 2
拿出 <-- 2
放在 --> 2
放在 --> 3
拿出 <-- 3
拿出 <-- 2
放在 --> 2
放在 --> 3
拿出 <-- 3
放在 --> 3
拿出 <-- 3
拿出 <-- 2
拿出 <-- 1
另外,團IDC網上有許多產品團購,便宜有口碑

10. JAVA多生產者多消費者問題。希望用wait()和notify().謝謝!



publicclassProceConsumerDemo{

publicstaticvoidmain(String[]args){
//1.創建資源
Resourceresource=newResource();
//2.創建兩個任務
Procerprocer=newProcer(resource);
Consumerconsumer=newConsumer(resource);
//3.創建線程
/*
*多生產多消費產生的問題:重復生產、重復消費
*/
Threadthread0=newThread(procer);
Threadthread1=newThread(procer);

thread0.setName("生產者(NO0)");
thread1.setName("生產者(NO1)");

Threadthread2=newThread(consumer);
Threadthread3=newThread(consumer);

thread2.setName("消費者(NO2)");
thread3.setName("消費者(NO3)");

thread0.start();
thread1.start();
thread2.start();
thread3.start();
}
}

classResource{
privateStringname;
privateintcount=1;
//定義標記
privatebooleanflag;

//提供給商品賦值的方法
publicsynchronizedvoidsetName(Stringname){//thread0,thread1在這里運行
while(flag)//判斷標記為true,執行wait等待,為false則生產
/*
*這里使用while,而不使用if的理由如下:
*
*thread0有可能第二次也搶到鎖的執行權,判斷為真,則有麵包不生產,所以接下來執行等待,此時thread0在線程池中。
*接下來活的線程有3個(除了thread0),這三個線程都有可能獲取到執行權.
*假設thread1獲得了執行權,判斷為真,則有麵包不生產,執行等待。此時thread1又進入到了線程池中。
*接下來有兩個活的線程thread2和thread3。假設thread2又搶到了執行權,所以程序轉到了消費get處……
*/
try{
this.wait();//這里wait語句必須包含在try/catch塊中,拋出異常。
}catch(InterruptedExceptione){
e.printStackTrace();
}
this.name=name+count;//第一個麵包
count++;//2
System.out.println(Thread.currentThread().getName()+this.name);//thread0線程生產了麵包1

//生產完畢,將標記改成true.
flag=true;//thread0第一次生產完麵包以後,將標記改為真,表示有麵包了

//喚醒消費者(這里使用notifyAll而不使用notify的原因在下面)

this.notifyAll();//第一次在這里是空喚醒,沒有意義
}

/*
*通過同步,解決了沒生產就消費的問題
*生產完以後,生產者釋放了this鎖,此時,生產者和消費者同時去搶鎖,又是生產者搶到了鎖,所以就出現了一直生產的情況。
*與「生產一個就消費一個的需求不符合」等待喚醒機制wait();該方法可以使線程處於凍結狀態,並將線程臨時存儲到線程池
*notify();喚醒指定線程池中的任意一個線程。notifyAll();喚醒指定線程池中的所有線程
*這些方法必須使用在同步函數中,因為他們用來操作同步鎖上的線程上的狀態的。
*在使用這些方法時候,必須標識他們所屬於的鎖,標識方式就是鎖對象.wait();鎖對象.notify();鎖對象.notifyAll();
*相同鎖的notify()可以獲取相同鎖的wait();
*/

publicsynchronizedvoidgetName(){//thread2,thread3在這里運行
while(!flag)
/*
*……接著上面的程序執行分析thread2拿到鎖獲取執行權之後,判斷!flag為假,則不等待,直接消費麵包1,輸出一次.
*消費完成之後將flag改為假接下來又喚醒了thread0或者thread1生產者中的一個
*假設又喚醒了thread0線程,現在活的線程有thread0,thread2,thread3三個線程
*假設接下來thread2又搶到了執行權,判斷!flag為真,沒麵包了,停止消費,所以thread2執行等待.
*此時活著的線程有thread0和thread3。
*假設thread3得到了執行權,拿到鎖之後進來執行等待,此時活著的線程只有thread0.
*所以thread0隻能搶到執行權之後,生產麵包2,將標記改為true告訴消費者有麵包可以消費了。
*接下來執行notify喚醒,此時喚醒休眠中的3個線程中的任何一個都有可能。
*如果喚醒了消費者thread2或者thread3中的任何一個,程序都是正常。如果此時喚醒thread1則不正常。
*如果喚醒了thread1,此時活著的線程有thread0和thread1兩個線程。
*假設thread0又獲得了執行權,判讀為真有麵包,則又一次執行等待。
*接下來只有thread1線程有執行權(此時沒有判斷標記直接生產了,出錯了),所以又生產了麵包3。在這個過程中,麵包2沒有被消費。
*這就是連續生產和消費容易出現的問題。
*
*原因:被喚醒的線程沒有判斷標記就開始執行了,導致了重復的生產和消費發生。
*
*解決:被喚醒的線程必須判斷標記,使用while循環標記,而不使用if判斷的理由。
*
*但是接下來會出現死鎖,原因在於:
*上面的程序中thread0在執行notify的時候喚醒了thread1,而此時thread2和thread3兩個消費者線程都處於等待狀態
*thread1在執行while判斷語句之後判斷為真,則執行等待,此時所有的線程都處於凍結等待狀態了。
*
*原因:本方線程在執行喚醒的時候又一次喚醒了本方線程,而本方線程循環判斷標記又繼續等待,而導致所有的線程都等待。
*
*解決:本方線程喚醒對方線程,可以使用notifyAll()方法
* 喚醒之後,既有本方,又有對方,但是本方線程判斷標記之後,會繼續等待,這樣就有對方線程在執行。
*/
try{
this.wait();
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+this.name);
//將標記改為false
flag=false;
//喚醒生產者
this.notify();
}
}

//生產者
classProcerimplementsRunnable{
privateResourceresource;

publicProcer(Resourceresource){
this.resource=resource;
}

publicvoidrun(){
while(true){
resource.setName("麵包");
}
}
}

//消費者
{
privateResourceresource;

publicConsumer(Resourceresource){
this.resource=resource;
}

@Override
publicvoidrun(){
while(true){
resource.getName();
}
}
}

熱點內容
在系統編程 發布:2024-04-19 08:54:55 瀏覽:234
visualstudio反編譯 發布:2024-04-19 08:44:46 瀏覽:319
ise怎麼配置晶元 發布:2024-04-19 08:27:31 瀏覽:997
免費搭建在線查詢伺服器 發布:2024-04-19 08:17:28 瀏覽:46
vs資料庫實例 發布:2024-04-19 08:14:54 瀏覽:295
vfp9反編譯 發布:2024-04-19 08:11:31 瀏覽:381
火車軟卧無線密碼是多少 發布:2024-04-19 07:38:59 瀏覽:423
vb系統文件夾 發布:2024-04-19 07:29:58 瀏覽:740
qt怎麼添加文件夾 發布:2024-04-19 07:22:53 瀏覽:256
sql查詢表是否存在 發布:2024-04-19 06:11:48 瀏覽:623