title: Android Bitmap multithreaded processing - not thread-safe?
body: <p>I want to process a bitmap in my Android application - the bitmap can be huge, so I use multithreading to perform faster operations. Here's my code (part of Runnable child):</p>
  
  <pre><code>@Override
  public void run() {
      int imageHeight = filter.getBitmap().getHeight();
      int start = threadNumber * imageHeight / threadCount;
      int stop = (threadNumber + 1) * imageHeight / threadCount;
      for (int j = start; j &lt; stop; j++) {
          filter.processLine(j);
      }
  }
  //...
  protected void processLine(int lineNumber)
  {
      int width = bitmap.getWidth();
      int pixels[] = new int[width];
      bitmap.getPixels(pixels, 0, width, 0, lineNumber, width, 1);
      for (int i = 0; i &lt; width; i++) {
          pixels[i] = processPixel(pixels[i]);
      }
      bitmap.setPixels(pixels, 0, width, 0, lineNumber, width, 1);
  }
  </code></pre>
  
  <p>When I use only 1 thread in the pool, everything works properely. Unfortunetely, when I use thread number equal to number of cores of the processor (4 in my device) the result is like following (for grayscale filter):</p>
  
  <p><img src="https://i.stack.imgur.com/JJY4Z.png" alt="issue"></p>
  
  <p>It looks like sometimes:</p>
  
  <ul>
  <li>bitmap.getPixels(...) does not work, as there are black lines in the output</li>
  <li>bitmap.setPixels(...) does not work, as there are unchanged lines in the output</li>
  </ul>
  
  <p>Am I right? Are these functions thread-unsafe? How should I perform multi-thread bitmap filtering to be fast and thread-safe?</p>