Producer and Consumer Threads Hang
Posted
by
user972425
on Stack Overflow
See other posts from Stack Overflow
or by user972425
Published on 2012-04-11T07:40:54Z
Indexed on
2012/04/11
11:29 UTC
Read the original article
Hit count: 268
So this is my first foray into threads and thus far it is driving me insane. My problem seems to be some kind of synchronization error that causes my consumer thread to hang. I've looked at other code and just about everything I could find and I can't find what my error is. There also seems to be a discrepancy between the code being executed in Eclipse and via javac in the command line.
Intention - Using a bounded buffer (with 1000 slots) create and consume 1,000,000 doubles. Use only notify and wait.
Problem - In Eclipse the consumer thread will occasionally hang around 940,000 iterations, but other times completes. In the command line the consumer thread always hangs.
Output - Eclipse - Successful
Producer has produced 100000 doubles.
Consumer has consumed 100000 doubles.
Producer has produced 200000 doubles.
Consumer has consumed 200000 doubles.
Producer has produced 300000 doubles.
Consumer has consumed 300000 doubles.
Producer has produced 400000 doubles.
Consumer has consumed 400000 doubles.
Producer has produced 500000 doubles.
Consumer has consumed 500000 doubles.
Producer has produced 600000 doubles.
Consumer has consumed 600000 doubles.
Producer has produced 700000 doubles.
Consumer has consumed 700000 doubles.
Producer has produced 800000 doubles.
Consumer has consumed 800000 doubles.
Producer has produced 900000 doubles.
Consumer has consumed 900000 doubles.
Producer has produced 1000000 doubles.
Producer has produced all items.
Consumer has consumed 1000000 doubles.
Consumer has consumed all items.
Exitting
Output - Command Line/Eclipse - Unsuccessful
Producer has produced 100000 doubles.
Consumer has consumed 100000 doubles.
Producer has produced 200000 doubles.
Consumer has consumed 200000 doubles.
Producer has produced 300000 doubles.
Consumer has consumed 300000 doubles.
Producer has produced 400000 doubles.
Consumer has consumed 400000 doubles.
Producer has produced 500000 doubles.
Consumer has consumed 500000 doubles.
Producer has produced 600000 doubles.
Consumer has consumed 600000 doubles.
Producer has produced 700000 doubles.
Consumer has consumed 700000 doubles.
Producer has produced 800000 doubles.
Consumer has consumed 800000 doubles.
Producer has produced 900000 doubles.
Consumer has consumed 900000 doubles.
Producer has produced 1000000 doubles.
Producer has produced all items.
At this point it just sits and hangs.
Any help you can provide about where I might have misstepped is greatly appreciated. Code - Producer thread
import java.text.DecimalFormat;+ " doubles. Cumulative value of generated items= " + temp)
import java.util.*;
import java.io.*;
public class producer implements Runnable{
private buffer produceBuff;
public producer (buffer buff){
produceBuff = buff;
}
public void run(){
Random random = new Random();
double temp = 0, randomElem;
DecimalFormat df = new DecimalFormat("#.###");
for(int i = 1; i<=1000000; i++)
{
randomElem = (Double.parseDouble( df.format(random.nextDouble() * 100.0)));
try {
produceBuff.add(randomElem);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
temp+= randomElem;
if(i%100000 == 0)
{produceBuff.print("Producer has produced "+ i ); }
}
produceBuff.print("Producer has produced all items.");
}
}
Consumer thread
import java.util.*;
import java.io.*;
public class consumer implements Runnable{
private buffer consumBuff;
public consumer (buffer buff){
consumBuff = buff;
}
public void run(){
double temp = 0;
for(int i = 1; i<=1000000; i++)
{
try {
temp += consumBuff.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i%100000 == 0)
{consumBuff.print("Consumer has consumed "+ i );
//if(i>999000)
//{System.out.println("Consuming item " + i);}
}
consumBuff.print("Consumer has consumed all items.");
}
}
Buffer/Main
import java.util.*;
import java.io.*;
public class buffer {
private double buff[];
private int addPlace;
private int getPlace;
public buffer(){
buff = new double[1000];
addPlace = 0;
getPlace = 0;
}
public synchronized void add(double add) throws InterruptedException{
if((addPlace+1 == getPlace) )
{
try {
wait();
} catch (InterruptedException e) {throw e;}
}
buff[addPlace] = add;
addPlace = (addPlace+1)%1000;
notify();
}
public synchronized double get()throws InterruptedException{
if(getPlace == addPlace)
{
try {
wait();
} catch (InterruptedException e) {throw e;}
}
double temp = buff[getPlace];
getPlace = (getPlace+1)%1000;
notify();
return temp;
}
public synchronized void print(String view)
{
System.out.println(view);
}
public static void main(String args[]){
buffer buf = new buffer();
Thread produce = new Thread(new producer(buf));
Thread consume = new Thread(new consumer(buf));
produce.start();
consume.start();
try {
produce.join();
consume.join();
} catch (InterruptedException e) {return;}
System.out.println("Exitting");
}
}
© Stack Overflow or respective owner