This discussion is archived
3 Replies Latest reply: Jan 26, 2010 8:36 AM by 843789 RSS

concurrent access to data structure by threads

843789 Newbie
Currently Being Moderated
Hi,
I have list of object which is read by a single thread but several writer threads can into it.
The reader reads the objects of the list and does some processing.
And the reader thread must wait for a notification from writer threads that the list has been written into.
So each writer thread writes into the list and then notifies the reader . Each time the reader receives a notification it starts its processing and then checks if all the writers are done, if they aren't then it waits again and if all the writers and done then its does a final procesing and exits.

I was able to synchronise the writers but i am not able to synchronize the reader thread and the writer threads.

my code is as follows :-

class Data
{
private List<String> resultList = Collections.synchronizedList(new ArrayList<String>());
private boolean modified = false;

public List<String> getResultList()
{
return this.resultList;
}

public void setResultList(List<String> resultList)
{
this.resultList = resultList;
}

public void addElementToResultList(String str)
{
this.resultList.add(str);
}

public synchronized boolean IsModified()
{
               return this.modified;
}
     
     public synchronized void setModified(boolean modified)
     {
          this.modified = modified;
     }
}



Class CallerTest
{
Data myData = new Data();
List<String> resultList = null
int counter = 0;

     public synchronized void incrementCounter()
     {
          this.counter++;
     }
     
     public synchronized int getCounter()
     {
          return this.counter;
     }

     public void setFinalResult(List<String> resultList )
     {
          this.resultList = resultList ;
     }
     
public void TestMeth()
{
     int numWriters = 5;
     ReaderThread rt = new rt.start(this,numWriters ,myData );
     rt.start();

     for(int i =0;i<numWriters ;i++)
     {
          WriterThread wt = new WriterThread (this,myData );
          wt.start();
     }
     
     try{
          rt.join();
     }
     catch(Exception x)
     {
     }

}


}

class ReaderThread extends Thread
{

     CallerTest caller = null;
     int numWriters=0;
     Data data = null;


public ReaderThread ()
{}

public ReaderThread (CallerTest caller,int numWriters,Data data)
{
     this.caller = caller;
     this.data = data;
     this.numWriters = numWriters;

}

public void run()
{
     try{
               while(!data.IsModified())
{
     data.getResultList().wait();
}
mergeResults(); // this is some processing
if(caller.getCounter() < this.numWriters)
data.setModified(false);
else
{
mergeResults();
caller.setFinalResult(finalResult);
}
}
}
catch(Exception ent)
{
log.error("The exception is in remote result reader ", ent);
}
}
}


class WriterThread extends Thread()
{

     private CallerTest caller = null;
     private Data data = null;

     WriterThread()
     {}

     WriterThread(CallerTest caller,Data data)
     {
          this.caller = caller;
          this.data = data;
     }

     public void run()
     {
          String str = getStringName()
          writeResult(str);
     }

     public String getStringName()
     {
          //some processing here
     }

     public synchronized void writeResult(String result)
          {
               
               try{
                    if(result != null)
                    {
                         data addElementToResultList(result);
                         caller.incrementCounter();
                         rrd.setModified(true);
                         data.getResultList().notifyAll();
                    }
               }
               catch(Exception ent)
               {
                    log.error("exception in WriterThread write remote results", ent);
               }
          }

}



however on running the program , i get an exception :-



[ERROR] [Mon Jan 25 12:13:49 2010] [Thread-59| WriterThread] exception in WriterThread write remote results
java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
WriterThread.writeResults(WriterThread.java:328)
WriterThread.run(WriterThread.java:83)

Please can you tell me how to synchronise the reader and writers.
greatly appreciate your help.


Thanks,
Helen

Edited by: hreese on Jan 25, 2010 11:08 AM