Java thread program works in Eclipse, but not in terminal / command prompt -


this code runs fine within eclipse, not in command prompt (or terminal). appreciated, have no idea why it's not working. runs way through in eclipse, hangs during execution in command prompt.

the producer class generates random doubles , calls add(), while consumer class calls pop(); both call these 1,000,000 times.

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"); } } 

you need synchronized pop , add methods. think should fix issue.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -