Java线程程序在Eclipse中工作,但不在终端/命令提示符下工作

时间:2011-04-12 00:01:35

标签: java eclipse

此代码在eclipse中运行正常,但在命令提示符(或终端)中运行不正常。任何帮助将不胜感激,因为我不知道为什么它不起作用。它在Eclipse中一直运行,但在命令提示符执行期间挂起。

Producer类生成随机双精度并调用add(),而Consumer类调用pop();他们都称这些为1,000,000次。

Buffer.java

public class Buffer{

private double[] buf;
private int next = 0;
private int start = 0;
private int semaphore = 1;
private boolean isFull = false;
private boolean isEmpty = true;

public static void main(String[] args) {
    Buffer pcbuf = new Buffer(1000);
    Thread prod = new Thread (new Producer(pcbuf));
    Thread cons = new Thread (new Consumer(pcbuf));
    prod.start();
    cons.start();
}

public Buffer(int size){
    buf = new double[size];
}

private synchronized void bwait(){
    while(semaphore <= 0){}
    semaphore--;
}

private void bnotify(){
    semaphore++;
}

public void add(double toAdd){
    boolean hasAdded = false;
    while(!hasAdded){
        if(!isFull){
            bwait();
            buf[next] = toAdd;
            next=(next+1)%buf.length;
            if(next == start){
                isFull = true;
            }
            isEmpty = false;
            hasAdded = true;
            bnotify();
        }
    }
}

public double pop(){
    boolean hasPopped = false;
    double toReturn = 0.0;
    while(!hasPopped){
        if(!isEmpty){
            bwait();
            toReturn = buf[start];
            start=(start+1)%buf.length;
            if(start == next){
                isEmpty = true;
            }
            isFull = false;
            hasPopped = true;
            bnotify();
        }
    }
    return toReturn;
}
}

Producer.java

import java.text.DecimalFormat;
import java.util.Random;

public class Producer extends Thread{

private Buffer b;
private double bufferValueCounter = 0.0;
private int numProduced = 0;

public Producer(Buffer b){
    this.b = b;
}

public void run() {
    Random r = new Random();
    DecimalFormat df = new DecimalFormat("#,###");
    while (numProduced < 1000000){
        double toAdd = r.nextDouble() * 100.0;
        b.add(toAdd);
        bufferValueCounter+=toAdd;
        numProduced++;
        if(numProduced%100000==0){
            System.out.println("Producer: Generated " + df.format(numProduced) + " items, Cumulative value of generated items = " + bufferValueCounter);
        }
    }
    System.out.println("Producer: Finished generating 1,000,000 items");
}   
}

Consumer.java

import java.text.DecimalFormat;

public class Consumer extends Thread{

private Buffer b;
private double bufferValueCounter = 0.0;
private int numConsumed = 0;

public Consumer(Buffer b){
    this.b = b;
}

public void run(){
    DecimalFormat df = new DecimalFormat("#,###");
    while(numConsumed < 1000000){
        double popped = b.pop();
        bufferValueCounter += popped;
        numConsumed++;
        if(numConsumed%100000==0){
            System.out.println("Consumer: Consumed  " + df.format(numConsumed) + " items, Cumulative value of consumed items  = " + bufferValueCounter);
        }
    }
    System.out.println("Consumer: Finished consuming 1,000,000 items");
}
}

1 个答案:

答案 0 :(得分:0)

您需要同步以弹出并添加方法。我认为应该解决这个问题。