多线程实现,生产者和消费者吃蛋糕问题。我认为问题出在类cake上,但是不会改,希望大神能给予帮助,谢谢

小仙女1228 2016-11-12 07:56:21
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();

}

}
...全文
268 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
package org.jms.demo; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class Test { /** * 生产Rube标识计数器 */ private final AtomicInteger calculator = new AtomicInteger(); /** * 堵塞队列,分离生产者消费者 */ private volatile BlockingQueue<Rube> queue = new ArrayBlockingQueue<Rube>(50); private volatile boolean isStop = true; private final ExecutorService executor = Executors.newFixedThreadPool(2); interface Food { } class Rube implements Food { public int getId() { return id; } public void setId(int id) { this.id = id; } private int id; public Rube(int id) { this.id = id; } } /* * 生產者 */ final class Producer implements Runnable { @Override public void run() { while (isStop) { if (calculator.get() == 100) { isStop = false; break; } Rube rube = new Rube(calculator.incrementAndGet()); try { System.out.println("Produce 生產了" + rube.getId()); queue.put(rube); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } /* * 消費者 */ final class Consumer implements Runnable { @Override public void run() { while (true) { try { if (isStop == false) { System.out.println("queue.size():" + queue.size()); synchronized (Consumer.class) { if (queue.size() == 0) { break; } } } Rube rube = queue.take(); System.out.println("Consumer消費:" + rube.getId()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } executor.shutdown(); } } public void init() { executor.execute(new Producer()); executor.execute(new Consumer()); } public static void main(String[] args) { Test test = new Test(); test.init(); } }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧