多线程实现,生产者和消费者吃蛋糕问题。我认为问题出在类cake上,但是不会改,希望大神能给予帮助,谢谢
package duoxiancheng;
public class cake {
private int id;
public cake(){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public class basket {
private cake[] cakes=new cake[10];
private int currentIndex=0;
public synchronized void push(cake cake){
while(currentIndex >= cakes.length){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
cakes[currentIndex]=cake;
currentIndex++;
System.out.println("放入:"+cake.toString());
notify();
}
public synchronized cake pop(){
while(currentIndex <= 0){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
currentIndex--;
cake temp=cakes[currentIndex];
System.out.println("吃掉:"+temp.toString());
notify();
return temp;
}
}
public class productor extends Thread {
private basket basket;
public productor(basket basket){
this.basket=basket;
}
public void run(){
while(true){
basket.pop();
//System.out.println("放入:"+cake.toString());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class customer implements Runnable{
private basket basket;
public customer(basket basket){
this.basket=basket;
}
public void run(){
while(true){
basket.pop();
//System.out.println("吃掉:"+cake.toString());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
cake cake=new cake();
basket basket=new basket();
productor productor=new productor(basket);
customer customer=new customer(basket);
productor.start();
Thread th=new Thread(customer);
th.start();
}
}
public class cake {
private int id;
public cake(){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public class basket {
private cake[] cakes=new cake[10];
private int currentIndex=0;
public synchronized void push(cake cake){
while(currentIndex >= cakes.length){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
cakes[currentIndex]=cake;
currentIndex++;
System.out.println("放入:"+cake.toString());
notify();
}
public synchronized cake pop(){
while(currentIndex <= 0){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
currentIndex--;
cake temp=cakes[currentIndex];
System.out.println("吃掉:"+temp.toString());
notify();
return temp;
}
}
public class productor extends Thread {
private basket basket;
public productor(basket basket){
this.basket=basket;
}
public void run(){
while(true){
basket.pop();
//System.out.println("放入:"+cake.toString());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class customer implements Runnable{
private basket basket;
public customer(basket basket){
this.basket=basket;
}
public void run(){
while(true){
basket.pop();
//System.out.println("吃掉:"+cake.toString());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
cake cake=new cake();
basket basket=new basket();
productor productor=new productor(basket);
customer customer=new customer(basket);
productor.start();
Thread th=new Thread(customer);
th.start();
}
}
...全文
相关推荐
cake_蛋糕_
例如,如果要制作一个使用500克面粉的蛋糕,那么需要250克砂糖,250克液体,和250克脂肪。7. **cake.fig和cake.m文件**:这两个文件可能包含更具体的信息。".
3个厨师做蛋糕,5个顾客吃蛋糕(java 线程问题)
在这个“3个厨师做蛋糕,5个顾客吃蛋糕”的例子中,我们可以通过多线程技术来模拟一个蛋糕店的运营场景,这有助于理解Java线程同步和资源管理的概念。
CakeEating:蛋糕吃问题的Python代码
蛋糕吃用于使用值函数迭代解决蛋糕食用问题的Python代码。 受到约翰·史陶斯基(John Stachurski)和托马斯·萨金特(Thomas Sargent)的定量经济学(QuantEcon)的启
dimensional-cake-rebuilt:您所吃的蛋糕可将您运送到各个维度,目前一直传到最后
重建立体蛋糕我看到Dimensional Cake从未移植到1.9.4以上。 它是MIT许可的,我问我是否可以接管它,以防万一有一些源代码可用。 我决定从头开始构建它,因为没有可用的源代码,并从中借用
Cake-Order-Sheet:帮助BR蛋糕装饰商跟踪蛋糕订单
下面,我们将深入探讨这个系统的功能、实现原理以及JavaScript在这个系统中的作用。首先,"Cake-Order-Sheet"的核心功能是订单管理。


Java SE
