java生產者消費者程式

2021-05-24 05:52:48 字數 915 閱讀 2808

package com.thread.sync;

//著名的生產者消費者程式

//生產者向容器裡放東西,同時消費者取出去

public class producerconsumer

}//產品類

class produce

@override

public string tostring()    

}//容器類,先進去後拿出來

class container catch (interruptedexception e)

}this.notifyall();

this.p[index] = p;

index ++;

}//從容器取方法

public synchronized produce pop() catch (interruptedexception e)

}// 喚醒在此物件監視器上等待的單個執行緒。

this.notifyall();

index --;

return p[index];}}

//生產者

class producer implements runnable

//生產產品的方法

public void produce() catch (interruptedexception e) }}

@override

public void run()

}//消費者

class consumer implements runnable

//消費產品的方法

public void consume() catch (interruptedexception e) }}

@override

public void run()

}

java生產者消費者

題目 請用多執行緒實現乙個生產者類和乙個消費者類,生產者隨機生成20個字元,消費者將字元列印到控制台。class syncstackcatch interruptedexception e this.notify 通知其它執行緒把資料出棧 buffer index c 資料入棧 index 指標向上...

生產者消費者 生產者與消費者模式

一 什麼是生產者與消費者模式 其實生產者與消費者模式就是乙個多執行緒併發協作的模式,在這個模式中呢,一部分執行緒被用於去生產資料,另一部分執行緒去處理資料,於是便有了形象的生產者與消費者了。而為了更好的優化生產者與消費者的關係,便設立乙個緩衝區,也就相當於乙個資料倉儲,當生產者生產資料時鎖住倉庫,不...

生產者消費者

using system using system.collections.generic using system.threading namespace gmservice foreach thread thread in producers q.exit console.read public...