0

i have two classes one is Manager and another one is parser. Parser class contain a method called getRecord(), which call a another method called getNextRecord().

There are two iterations in getNextRecord() method i.e. parent Iteration and child Iteration, hear parent Iteration iterate for 100 times and on every parent Iteration child iterate for 10 times by printing a number of child iteration.

now what i need is when i call a getRecord() method from Manager class only one child iteration should happen by printing a iteration message meaning parent Iteration will start and child iterate for one time and print SYSO statement there after loop should pause and main thread will go for waiting state without braking iteration / loop.

Again when ever i call getRecord() method it should print one child iteration and wait until i call getRecord() method next time with out breaking a loop

What i required is when ever i call getRecord() method it should print only one message with out breaking while loops present in Parser class.

while implementing this i am getting exception IllegalMonitorStateException hoq can i solve this

Manager class

import java.util.Properties;

public class Manager {

    public static void main(String[] args)
       {
          Properties properties = new Properties();

          Parser parser = new Parser(properties);

          parser.getRecord();
          parser.getRecord();
          parser.getRecord();
          parser.getRecord();

          /*Thread tProducer = new Thread(new Parser(properties));
          tProducer.start();*/
       }
}

Parser class

import java.util.Properties;

public class Parser extends Thread
{
       private Properties properties;

       Parser parser = null;

       private int parentIteration = 0;

       public Parser(Properties properties)
       {
          this.properties = properties;
          parser = new Parser();
       }

       public Parser(){

       }

       public void getNextRecord()
       {
          while (parentIteration <= 100) {

             try {

                 int childIteration = 0;

                 while (childIteration <= 10) {

                    System.out.println("Record "+ ++childIteration);
                    parser.wait();
                 }

             } catch (InterruptedException ex) {

                ex.printStackTrace();
             }

             ++parentIteration;
          }
       }

       void getRecord()
       {
           getNextRecord();
           parser.notify();
       }
    }

any help will be appreciated.Thanks.

Stack trace

Record 1Exception in thread "main" java.lang.IllegalMonitorStateException

    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)
    at Parser.getNextRecord(Parser.java:32)
    at Parser.getRecord(Parser.java:46)
    at Manager.main(Manager.java:11)
Rajesh Hatwar
  • 1,843
  • 6
  • 39
  • 58

0 Answers0