title: main thread consumer and other threads producer
body: <p>My questions is, I have a data set of 1000 records. I want 3 threads that process the data like this,
  thread1 from record 1 to 300, thread2 from 301 to 600 and so on. One thread can make a request and fetch 50 records at a time, create an object and put it in a queue.
  Main thread will simultaneously read data from the queue.</p>
  
  <p>Below is the code, the problem I am facing is that recordRead variable tells the starting point from where the thread should start reading the records. 
  But how can I set different value for each thread e.g for thread1 it should be 0 and recordsToRead should be 300 and for thread2, recordRead should be 300 and recordsToRead to be 300+300=600 and for last thread it should be 600 and upto the end. 
  pagesize=50
  pagesize,recordRead and recordToRead are all variables that belong to main class and main thread.</p>
  
  <pre><code>    ExecutorService service = Executors.newFixedThreadPool(nThreads);
      while(nThreads &gt; 0) {
          nThreads--;
          service.execute(new Runnable() {
  
              @Override
              public void run() {
                  // TODO Auto-generated method stub
  
                  do {
                      int respCode = 0;
                      int RecordsToRead = div;
                      JSONObject jsObj = new JSONObject();
                      jsObj.put("pagesize", pageSize);
                      jsObj.put("start", recordsRead);
                      jsObj.put("searchinternalid", searchInternalId);
  
                      try {
                          boolean status = req.invoke(jsObj); 
                          respCode = req.getResponseCode();
  
                      } catch (Exception e) {         
                          req.reset();
                          e.printStackTrace();
                          return true;
                      }
                      JSONObject jsResp = req.getResponseJson();
                      //here jsResp will be added to ArrayBlockingQueue.
  
                      req.reset();
                  }while(!isError &amp;&amp; !isMaxLimit &amp;&amp; recordsRead &lt; RecordsToRead);
  
              }
  
          });
      }
  </code></pre>
  
  <p>After this loop will be the code of main thread reading the queue.
  how can I set recordsRead and recordToread for all threads.</p>
  
  <p>And how to make main thread wait untill atleast one thread inserts an object in queue.</p>