title: Java: time to read sound from TargetDataLine and write to ByteArrayOutPutStream different sometimes
body: <p>I have a thread where I am reading data from TargetDataLine (microphone) and writing it to a ByteArrayOutputStream. I have a little "timer" because I want to record sound for 10 ms and then pass the captured data to another object. No problems so far, this code captures audio for 10 ms and passes the data. But sometimes, the while loop takes ~130 ms... Anyone out there who knows why this happens? Here is my code:</p>
  
  <pre><code>while (true) {
              byte tempBuffer[] = new byte[2];
              try {
                  byteArrayOutputStream = new ByteArrayOutputStream();
                  start = System.nanoTime();
  
                  double recording = System.nanoTime();
                  // Get microphone input
                  while (System.nanoTime() - start &lt;= 10 * 1000000) {
                      int count = targetDataLine.read(tempBuffer, 0,
                              tempBuffer.length);
  
                      if (count &gt; 0) {
                          byteArrayOutputStream.write(tempBuffer, 0, count);
                      }
                  }
  
                  byteArrayOutputStream.close();
  
                  LLCapturedData.push(byteArrayOutputStream.toByteArray());
  
                  byteArrayOutputStream.flush();
  
                  // Tell observers, that input is captured
                  setChanged();
                  notifyObservers();
              } catch (Exception e) {
                  // TODO
              }
          }
  </code></pre>