Android: synchronization issue caused by drawing to Canvas in a non-ui thread?
<p>My app has a layout that contains some ImageButtons.  In addition, my app renders some Bitmaps    for later use.  The Bitmaps are rendered by creating a Canvas with a Bitmap parameter, and then drawing into the canvas.  This Bitmap rendering task occurs in a non-ui thread, so the gui can remain responsive while the bitmaps render.</p>

<p>ANRs occurring.  The caused appears to be that the ImageButton.draw() method does not complete.  Additionally, it appears that the method fails to complete because the Bitmap rendering task begins rendering while the ImageButton is drawing.  </p>

<p>Below are some code snippets, by log output, and the traces.txt file from the ANR.  </p>

<p>Is my conclusion possible/likely, that drawing to a Canvas in a non-ui thread could cause ImageButton.draw() to hang?  If so, any suggestions to fix this?</p>

<pre><code>private class BitmapRenderTask implements Runnable
{
    ...     
    public void run()
    {
        Log.e("start render task", rect.toString());
        ...
        Canvas canvas = new Canvas(bitmap);
        Log.e("created canvas", "created canvas");
        //do stuff to draw into canvas
        Log.e("drew to canvas", "drew to canvas");
        ...
        Log.e("done, exit", rect.toString());

public class MyImageButton extends ImageButton
{ 
    public void draw(Canvas canvas)
    {
        Log.e("start drawing", this.toString());
        super.draw(canvas);
        Log.e("end drawing", this.toString());
    }
}

My log:

04-24 14:10:12.115: E/start render task(9446): 1 Rect(512, 0 - 800, 512)
04-24 14:10:12.230: E/start drawing(9446): MyButton1
04-24 14:10:12.230: E/end drawing(9446): MyButton1
04-24 14:10:12.230: E/start drawing(9446): MyButton2
04-24 14:10:12.230: E/end drawing(9446): MyButton2
04-24 14:10:12.230: E/start drawing(9446): MyButton3
04-24 14:10:12.230: E/end drawing(9446): MyButton3
04-24 14:10:12.230: E/start drawing(9446): MyButton4
04-24 14:10:12.235: E/end drawing(9446): MyButton4
04-24 14:10:12.235: E/start drawing(9446): MyButton5
04-24 14:10:12.235: E/end drawing(9446): MyButton5
04-24 14:10:12.235: E/start drawing(9446): MyButton6
04-24 14:10:12.390: E/drew to canvas(9446): drew to canvas
04-24 14:10:12.395: E/done, exit(9446): Rect(512, 0 - 800, 512)
04-24 14:10:12.395: E/start render task(9446): Rect(0, 1024 - 512, 1084)
04-24 14:10:12.410: E/drew to canvas(9446): drew to canvas
04-24 14:10:12.395: E/done, exit(9446): Rect(0, 1024 - 512, 1084)
</code></pre>

<p>traces.txt:</p>

<p><a href="http://pastebin.com/s98nEyYt" rel="nofollow">http://pastebin.com/s98nEyYt</a></p>
