-2

Do I need to add any packages for the below code to execute successfully? I am getting errors in the code that I have been unable to fix, particularly with reference to using the synchronized keyword. Can anyone point out what I am doing wrong? Thank you.

Data object:

class Q
{
    int n;
    boolean valueset=false;

    synchronized int get()
    {
        if(!valueset)

            try
            {
                wait();
            }
            catch(InterruptedException e)
            {
                System.out.println("Interrupted Exception Caught.");
            }


            System.out.println("Got:"+n);
            valueset=false;
            notify();
            return n;

    }

    synchronized void put(int n)
    {
        if(valueset)

            try
            {
                wait();
            }
            catch(InterruptedException e)
            {
                System.out.println("Interrupted Exception Caught.");
            }

            this.n=n;
            valueset=true;
            System.out.println("Put:"+n);
            notify();

    }
}

Producer:

class Producer implements Runnable
{
    Q q;
    Producer(Q q)
    {
        this.q=q;
        new Thread(this,"Producer").start();
    }

        public void run()
        {
            int i=0;
            while(true)
            {
                q.put(i++);
            }
        }

}

Consumer:

class Consumer implements Runnable
{
    Q q;
    Consumer(Q q)
    {
        this.q=q;
        new Thread(this,"Consumer").start();
    }   

        public void run()
        {
            while(true)
            {
                q.get();
            }
        }

}

Extra class:

class PCfixed 
{
    public static void main(String[] args) 
    {
        Q q=new Q();
        new Producer(q);
        new Consumer(q);

        System.out.println("Press Control-C to stop.");
    }
}
Teja
  • 13,214
  • 36
  • 93
  • 155

2 Answers2

0

It might be impossible to answer correctly without more information, but the following is definetly an issue [though might not be what you are encountering]:
You are not calling wait() in a loop! The javedocs clearly indicates

...applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops...

More info can be found in this thread

Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
  • _Certainly_ _agreed_ in principle but since there is only one consumer I don't think this is an issue in this case. – Gray Feb 22 '12 at 19:03
  • For your amusement see the wrong accepted answer here: http://stackoverflow.com/questions/2960581/why-does-java-util-concurrent-arrayblockingqueue-use-while-loops-instead-of-i – Gray Feb 22 '12 at 19:03
0

In both Producer and Consumer, you are trying to define a method public void run() inside the constructor. The curly braces are in the wrong place.

Alex D
  • 29,755
  • 7
  • 80
  • 126
  • Yes.. the braces were at wrong place.But still I get a logical error during run-time.My program doesn't print any output. – Teja Feb 22 '12 at 19:34
  • If you run the program using your IDE's debugger, you should see what threads are running. Do both the consumer and producer threads appear? If one or both threads are waiting and never waking up, you should be able to see that too. – Alex D Feb 22 '12 at 21:18