I have some code that stores MotionEvents that occur to a particular View (call this the producer), and some different code in another thread that reads that data for display purposes (call this the consumer). I am in essence re-creating down-one-color-up-a-different-color button functionality where using standard Android buttons is not feasible. The MotionEvents are stored in a single ArrayList. The accesses to this are wrapped in a single permit semaphore to prevent the provider and consumer from I/Oing to the ArrayList at the same time and reading transitory data. All well and good.
The problem is when I'm tapping on this View at high speed. On the producer side, I add stuff to the ArrayList in response to a MotionEvent.ACTION_DOWN, and I remove it from the list in response to an MotionEvent.ACTION_UP (ACTION_MOVEs I ignore). If im flooding the phone with taps, when it comes time to add/remove, the semaphore not available, either because my consumer is reading the ArrayList, or because I'm not done handling a previous tap.
So what is best practice for this problem? If I miss a MOTION_DOWN, it seems like I can safely ignore it, but if I miss a MOTION_UP, won't my consumer be stuck displaying the button like the finger was still pressed down? I am seeing an occasional display bug (nothing to do with rapid taps) that could be caused by a missed MOTION_UP.