Satish Patel
Satish Patel

Reputation: 1844

Why my producer thread is not completing its task?

I am working on the producer-consumer problem in Java in which the producer is writing Fibonacci numbers in the pipe and the consumer is consuming it via pipe reader and checking whether it is prime or not.

The problem is that only first 3 Fibonacci primes are generated by the code given below.

What is wrong with it?

package fibonacciprime;
import java.io.*;
import java.lang.*;
public class FibonacciPrime extends Thread {
    public static void main(String[] args) throws Exception {
        //final PipedOutputStream pout=new PipedOutputStream();
        //final PipedInputStream pin=new PipedInputStream();
        final PipedWriter pwriter = new PipedWriter();
        final PipedReader preader = new PipedReader(pwriter);
        //pout.connect(pin);
        Thread threadA=new Thread()
        {
            public void run()
            {
                for(int i=2;i<1000;i++)
                {
                    synchronized(pwriter)
                    {
                        try
                        {
                            int temp=5*i*i-4;
                            int temp1=5*i*i+4;
                            int p=(int)Math.sqrt(temp1)*(int)Math.sqrt(temp1);
                            int q=(int)Math.sqrt(temp)*(int)Math.sqrt(temp);

                            if(p==temp1 || q==temp)
                                pwriter.write(i);

                        }catch(Exception e){e.printStackTrace();}
                    }
                }
                try {
                    pwriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        Thread threadB = new Thread()
        {
            public void run()
            {
                int flag=0;
                try
                {
                    int temp;
                    while( ( temp = preader.read() ) != -1)
                    {
                        //int k=pin.read();
                        for(int i=2;i*i < temp;i++)
                        {
                            if(temp%i==0)
                            {
                                flag=1;
                                break;
                            }
                        }
                        Thread.sleep(100);
                        if(flag==0)
                            System.out.println(temp);
                    }
                    preader.close();
                }catch(Exception e){e.printStackTrace();}
            }
        };
        threadA.start();
        threadB.start();
    }
}

Upvotes: 3

Views: 87

Answers (1)

azurefrog
azurefrog

Reputation: 10955

Your producer thread is completing its task, but your consumer is buggy, so it doesn't print the appropriate values.

You declare your flag for detecting a prime number outside your while-loop, and never reset its value. Because of this, once the first non-prime number is read (8), all numbers after that will be treated as composite, even when they are prime.

You just need to move the declaration of flag inside your while-loop and your program will work:

while ((temp = preader.read()) != -1) {
    int flag = 0;  // moved this to inside the loop
    for (int i = 2; i * i < temp; i++) {
        if (temp % i == 0) {
            flag = 1;
            break;
        }
    }
    Thread.sleep(100);
    if (flag == 0) System.out.println(temp);
}

Upvotes: 4

Related Questions