程序如下:
public class test{
public static void main(String[] args){
Box b = new Box();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
new Thread(p).start();
new Thread(c).start();
}
}
class SteamedBun{
int id;
SteamedBun(int id){
this.id = id;
}
public String toString(){
return "SteamBun:" + id;
}
}class Box{
int index = 0;
int count = 0;
SteamedBun[] arrsb = new SteamedBun[6];
public synchronized void pull(SteamedBun sb){
while(index == arrsb.length){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notify();
arrsb[index] = sb;
System.out.println("生产了" + sb);
index ++;
}
public synchronized void pop(SteamedBun sb){
while(index == 0){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
} this.notify();
index --;
arrsb[index] = sb;
System.out.println("消费了" + sb);
}
}
class Producer implements Runnable{
Box b = null;
Producer(Box b){
this.b = b;
}
public void run(){
for(int i = 0;i < 10;i++){
SteamedBun sb = new SteamedBun(i);
b.pull(sb);
try {
Thread.sleep(1000);
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
Box b = null;
Consumer(Box b){
this.b = b;
}
public void run(){
for(int i = 0;i < 10;i++){
SteamedBun sb = new SteamedBun(i);
b.pop(sb);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}输出地结果比如是生产1,2,之后消费是1,2
但是我想要的是生产后的消费时栈一样的消费,后生产的先消费!
谢谢各位大大了 !
解决方案 »
- SwingWorker和周期性任务
- java中一个小Applet程序的疑问
- JBossOpenTool .jar 这个文件倒底在用jbuilder + jboss 中用不用啊!!!
- JComboBox内容显示有时候不出来怎么办啊
- jdk是不是会自动升级啊,怎么查看版本呢
- 谁能提供几个经典的SQL面试题?
- 学习lucene源码的疑问11111111111111111111111111
- 对一个表达式的解析求值,有没有什么好的算法????
- 送分啦!一个关于变量定义的简单问题!
- 我已经搞清楚了csdn为什么你有很多可用分却只可以给20的原因了!
- JAVA图片操作颜色失真
- properties文件,无法打入jar包
我没有明白这句话是什么意思生产消费者 java是可以通过阻塞队列来做的,而且效果特别好.
public static void main(String[] args){
Box b = new Box();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
new Thread(p).start();
new Thread(c).start();
}
} @SuppressWarnings("rawtypes")
class SteamedBun extends Stack{
/**
*
*/
private static final long serialVersionUID = 1L;
int id;
SteamedBun(){
}
SteamedBun(int id){
this.id = id;
}
public String toString(){
return "SteamBun:" + id;
}
} class Box{
int index = 0;
int count = 0;
SteamedBun arrsb = new SteamedBun();
@SuppressWarnings("unchecked")
public synchronized void pull(SteamedBun sb){
while(index == 6){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notify();
arrsb.push(sb);
System.out.println("生产了" + sb);
index ++;
}
public synchronized void pop(){
while(index == 0){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notify();
index --;
SteamedBun sb = (SteamedBun)arrsb.pop();
System.out.println("消费了" + sb);
}
}
class Producer implements Runnable{
Box b = null;
Producer(Box b){
this.b = b;
}
public void run(){
for(int i = 0;i < 10;i++){
SteamedBun sb = new SteamedBun(i);
b.pull(sb);
try {
Thread.sleep(1000);
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
Box b = null;
Consumer(Box b){
this.b = b;
} public void run(){
for(int i = 0;i < 10;i++){
b.pop();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}