0

Really need your help to figure out what is going on. The code seem to be so trivial, but it produces wrong output.

It is a basic producer-consumer problem. Generator thread generates prime numbers and consumer (acceptors) must process them. In App I created two acceptors and 1 generator thread. The problem is that I get the following output for 50 as a bound:

Thread Thread-2 puts 47
Thread Thread-2 puts 43
Thread Rob gets 47
Thread Rob gets 43
Thread Thread-1 gets 47
Thread Nick puts 47
etc...

I have no idea why Thread-2 and Thread-1 are printed... where these threads are coming from? Thanks

public class PrimeGenerator implements Runnable{
    private PrimeCentral primeCentral;
    int bound;

    public PrimeGenerator(PrimeCentral PC, int bound){
        this.primeCentral = PC;
        this.bound = bound;
        new Thread(this).start();
    }

    @Override
    public void run() {
          int n=bound;
          while (n > 1) {
             int d;
             for (d=2; d <= n/2; d++) 
                if ((n % d) == 0) break; 
             if (d > n/2) {
                 primeCentral.put(n);
                System.out.println("Thread " + Thread.currentThread().getName() + " puts " + n);
             }
          n--; 
          }

    }

}

public class PrimeCentral {
    private int prime;
    private boolean available = false;


    public synchronized void put(int n){
        while(available == true){
            try{
                wait();
            }catch(InterruptedException e){}
        }
        prime = n;
        available = true;
        notifyAll();        
    }

    public synchronized int get(){
        while(available == false){
            try{
                wait();
            }catch(InterruptedException e){}
        }
        available = false;
        notifyAll();
        return prime;
    }

}
public class PrimeAcceptor implements Runnable{
    private PrimeCentral primeCentral;

    public PrimeAcceptor(PrimeCentral primeCentral){
        this.primeCentral = primeCentral;
        new Thread(this).start();
    }


    public void run() {
          while (true) {
              int prime = primeCentral.get();
              System.out.println("Thread " + Thread.currentThread().getName() + " gets " + prime);
              if (prime == 2) break; 
          }
    }

public class App {

    public static void main(String[] args) {

        PrimeCentral pc = new PrimeCentral();

        new Thread(new PrimeAcceptor(pc), "Bob").start();
        new Thread(new PrimeAcceptor(pc), "Rob").start();

        new Thread(new PrimeGenerator(pc, 50), "Nick").start();

    }
}

EDIT: sorry people I was dumb with this stupid mistake

YohanRoth
  • 3,153
  • 4
  • 31
  • 58
  • 2
    [Do not start a thread from a constructor.](http://stackoverflow.com/questions/8057510/java-starting-a-new-thread-in-a-constructor) – mkobit May 04 '15 at 23:49

1 Answers1

2

You're starting two threads with new Thread(this).start().

They will both have names of the form you mention.

I think you're starting four threads where you only meant to start two. Delete the above line in both places it occurs.

user207421
  • 305,947
  • 44
  • 307
  • 483