Why isn't my production thread doing its job?

I am working on a producer-consumer problem in Java where the producer writes Fibonacci numbers in a pipe and the consumer consumes it through a pipe reader and checks if it is simple or not.

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

What's wrong with him?

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

      

+3


source to share


1 answer


Your producer thread is doing its job, but your consumer is faulty, so it doesn't print the appropriate values.

You declare your flag to define a prime outside of your while loop and never reset its value. Because of this, as soon as the first number without a space is read (8), all numbers after that will be treated as composite, even when they are primary.



You just need to move the declaration flag

inside the 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);
}

      

+4


source







All Articles