title: Multiple Threads in Java for ConcurrentHashMap
body: <p>I made a two thread one is get Data and another is save Data.
  <strong>My problem is it is not handled in the process of storing the data read from Thread1.</strong></p>
  
  <p>I want to extract 1,000,000 elements and create them as file. The element size is so big, So i divide a elements size by 100,000. And then, the loop will run a 10 time. One thread reads a data from other server by 100,000. Another thread takes the data from the first thread and writes it to a file.</p>
  
  <p>My Original scenario is below:</p>
  
  <p>First thread read a total key, value size.
  It will be 100,000 ~ 1,000,000. I would assume that i would process 1,000,000 data.Then Count sets 1,000,000. First Thread divide by 100,000 and read a data from server by 100,000. And then, First Thread calls a setData(Key,Value map).
  It will loop 10 times.</p>
  
  <p>Second Thread will loop 10 time. first, get a data by calling getMap() method. And It calls writeSeq(hashmap) method. It writes a data to writer stream. It is not yet flush. There is a problem here. It successfully gets a data size by calling getMap(). But, writeSeq method can not process all of size of value. When i gets a size of 100,000, it process as random. It will be 100, 1500, 0, 8203 ...</p>
  
  <p>First Thread is below:</p>
  
  <pre><code>public void run() {
          getValueCount(); //initialize value.
  
          while (this.jobFlag) {
              getSortedMap(this.count); //count starts the number of all elements size.
  //For example, Total size is 1,000,000. Then count will sets a 1,000,000 and it is decreased as 100,000.
  // Also setMap() is called in this method.
              if (!jobFlag) //If all processing is done, jobFlag is set as false.
                  break;
          }
  
          resetValue();
      }
  </code></pre>
  
  <p>Second Thread is below:</p>
  
  <pre><code>public void run() {
          setWriter(); //Writer Stream creates;
  
          double count  = 10; //the number of loop. 
  
          ConcurrentHashMap&lt;String, String&gt; hash = new ConcurrentHashMap&lt;String,String&gt;();
  
          for (int i = 0; i &lt;= count - 1; i++) {
              hash = share.getMap();
              writeSeq(hash);
          }
  
          closeWriter(); //close Writer stream
      }
  </code></pre>
  
  <p>This is shared source:</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.concurrent.ConcurrentHashMap;
  
  public class ShareData {
  
      ConcurrentHashMap&lt;String, String&gt; map;
  
      public synchronized ConcurrentHashMap&lt;String, String&gt; getMap(){
          if (this.map == null) {
              try {
                  wait();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
          ConcurrentHashMap&lt;String, String&gt; hashmap = map;
  
          this.map = null;
  
          return hashmap;
      }
  
      public synchronized void setMap(ConcurrentHashMap&lt;String, String&gt; KV) {
          if (this.map != null) {
              try {
                  wait();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
          this.map = KV;
          notify();
      }
  
  }
  </code></pre>
  
  <p>After that, second thread which save a data is stared. <strong>The size of KV is fine, but all values are not processed when foreach is proecessed.</strong> Also, each time i create a file, the size is different. Is it problem of synchronized?</p>
  
  <pre><code>public synchronized void writeSeq(ConcurrentHashMap&lt;String, String&gt; KV) {
  
          AtomicInteger a = new AtomicInteger(0);
          System.out.println(KV.size()); //ex) 65300
          redisKV.entrySet().parallelStream().forEach(
                  entry -&gt; { 
                      try {
                          a.incrementAndGet();
                          writer.append(new Text(entry.getKey()), new Text(entry.getValue()));
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  });
          System.out.println(a.get()); //ex) 1300
          i = 0;
          notify();
  
      }
  </code></pre>