Java?殻?旺窟亅恬-伏恢宀?継宀庁侏
扮寂:2011-02-20 51cto鴬人 leizhimin
糞縞貧?彈鳩傍哘乎頁“伏恢宀-?継宀-花刈”庁侏?宣蝕阻花刈?伏恢宀?継宀庁侏祥?誼短嗤傍捲薦阻。
斤噐緩庁侏?哘乎苧鳩匯和叱泣?
1、伏恢宀叙叙壓花刈隆諾扮昨伏恢?花諾夸唯峭伏恢。
2、?継宀叙叙壓花刈嗤恢瞳扮昨嘉嬬?継?花腎夸吉棋。
3、輝?継宀窟?花刈短恢瞳辛?継扮昨氏宥岑伏恢宀伏恢。
4、伏恢宀壓伏恢竃辛?継恢瞳扮昨?哘乎宥岑吉棋議?継宀肇?継。
緩庁侏繍勣潤栽java.lang.Object議wait嚥notify、notifyAll圭隈栖糞?參貧議俶箔。宸頁掲械嶷勣議。
/**
* Java?殻?旺窟亅恬-伏恢宀?継宀庁侏
*
* @author leizhimin
*/
public class Test {
public static void main(String[] args) {
Godown godown = new Godown(30);
Consumer c1 = new Consumer(50, godown);
Consumer c2 = new Consumer(20, godown);
Consumer c3 = new Consumer(30, godown);
Producer p1 = new Producer(10, godown);
Producer p2 = new Producer(10, godown);
Producer p3 = new Producer(10, godown);
Producer p4 = new Producer(10, godown);
Producer p5 = new Producer(10, godown);
Producer p6 = new Producer(10, godown);
Producer p7 = new Producer(80, godown);
c1.start();
c2.start();
c3.start();
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
p7.start();
}
}
/**
* 花垂
*/
class Godown {
public static final int max_size = 100; //恷寄垂贋楚
public int curnum; //輝念垂贋楚
Godown() {
}
Godown(int curnum) {
this.curnum = curnum;
}
/**
* 伏恢峺協方楚議恢瞳
*
* @param neednum
*/
public synchronized void produce(int neednum) {
//霞編頁倦俶勣伏恢
while (neednum + curnum > max_size) {
System.out.println("勣伏恢議恢瞳方楚" + neednum + "階狛複噫垂贋楚" + (max_size - curnum) + "?壙扮音嬬峇佩伏恢販暦!");
try {
//輝念議伏恢?殻吉
|