Java Threads Consumer Algorithm Not Working Properly

I am trying to study topics, and so I wrote an example consumer-producer problem in which the manufacturer produces numbers from 1 to 10, and the consumer should display them. But only the consumer shows the number 1 and stops.

As I said, the program is poorly written and can be absurd, but still I want to find out the reason why all numbers from 1 to 10 are not printed, because I will remember better when I code, and not from examples.

I use two varibales to track the progress of Producres or consumers so that I can execute the others.

/ getStatus (consumer C) passing the link to the consumer so that the producer gets refrnce for the consumer .. and it can be used to determine the status of the consumer .. this is it .. /

import java.lang.Math;
public class Hello{

public static void main(String args[]) {

    System.out.println("---1");
    new Consumer().start();

}

}

class Producer extends Thread{

public int produce = 0;
public Consumer consumerObj =null;
int count = 1;
boolean producerStatus = false;

public void run(){

    System.out.println("---4");
    synchronized(this){

        do{

            System.out.println("---6");
            produce = produce+1;
            producerStatus = true;
            notify();
            consumerObj.consumerStatus = false;
            System.out.println("---9");
            count = count+1;

        }while(count<=10 && consumerObj.getStatus());

    }

}

public int getItem(){
    return produce;
}



public boolean getStatus(Consumer c){
    consumerObj = c;
    return producerStatus;

}

}

class Consumer extends Thread{

boolean consumerStatus = false;
int count =0;
public void run(){

    System.out.println("---2");
    Producer p = new Producer();
    p.getStatus(this);
    p.start();
    try{
        System.out.println("---3");
        synchronized(p){
            System.out.println("---5");
            p.wait();

            System.out.println("---8");
        }
    }
    catch(Exception e){
            System.out.println("exception");
    }

    synchronized(p){
        try{
            while(p.getStatus(this) && count<=9 ){

                System.out.println("---7");
                int consume = p.getItem();
                System.out.println("the produced item is ----->"+consume);
                count = count+1;
                p.producerStatus  = false;
                consumerStatus = true;
                p.wait();                   
                System.out.println("---10");

            }
        }
        catch(Exception e){
            System.out.println("exception");
        }

    }

}

public boolean getStatus(){

    return consumerStatus;

}
}

Output:

---1
---2
---3
---5
---4
---6
---9
---8
---7
the produced item is ----->1
---10

After entering from .. Suraj .. Now the program works fine .. see below ..

import java.lang.Math; public class Hello {

public static void main(String args[]) {

    System.out.println("---1");
    new Consumer().start();

}

}

class Producer extends Thread{

public int produce = 0;
public Consumer consumerObj =null;
int count = 1;
boolean producerStatus = false;

public void run(){

    System.out.println("---4");

        do{
            if(consumerObj.getStatus()){


            System.out.println("---6");
            produce = produce+1;
            System.out.println("---9 -- >produce is -->"+produce);
            producerStatus = true;
            synchronized(this){

            notify();
            System.out.println("---6.111");
            }

            consumerObj.consumerStatus = false;
            count = count+1;


            }


        }while(count<=10);


}

public int getItem(){

    return produce;

}

public boolean getStatus(Consumer c){

    consumerObj = c;
    return producerStatus;

}

}



class Consumer extends Thread{

boolean consumerStatus = true;
int count =1;
public void run(){

    System.out.println("---2");
    Producer p = new Producer();
    p.getStatus(this);
    p.start();//can a thread1 wait on an thread2 before the thread2 hass tarted and in this case wll notify on the scnd thread reaally notify therad1 ..
    try{
        System.out.println("---3");
        synchronized(p){
            System.out.println("---5");
            p.wait();

            System.out.println("---8");
        }
    }
    catch(Exception e){
            System.out.println("exception");
    }


        try{
            while(count<=10 ){

                System.out.println("---7");
                int consume = p.getItem();
                System.out.println("the produced item is ----->"+consume);
                count = count+1;
                p.producerStatus  = false;
                consumerStatus = true;
                synchronized(p){
                p.wait();   
                System.out.println("---10");        
                }


            }
        }
        catch(Exception e){
            System.out.println("exception");
        }


}

public boolean getStatus(){

    return consumerStatus;

}

}
+3
source share
3 answers

OK The program is incorrect.

  • Consumer flow comes in and waits for a lock.
  • The first iteration in the producer loop will cause a notification, but will release the lock because the while loop is inside the synchronized block. It will be executed 10 times, without releasing the lock and ending with the fact that they cause a notification 10 times continuously.
  • , , wait(), , ( 10 Producer )

, , , , 10 .

+3

consumerObj.consumerStatus = false;

. 1 . ,

count = count+1;

( "" ).

try{
    this.wait();
} catch (Exception e) {
    e.printStackTrace();
}

.

p.wait();
0

BlockingQueues. . http://www.javamex.com/tutorials/synchronization_producer_consumer_2.shtml.

The above implementation is inefficient and causes all consurmers to block. with multiple queues you can prevent all consumers from blocking. You can find examples on google. Java.util.concurrent Use it, it is for some reason.

0
source

All Articles