android thread and handler best practices
<p>I have the following class.  The purpose of the class is to allow me to simulate a teletype/typewriter by displaying about ten characters a second.  </p>

<p>The point of the CharacterLoopThread class is to look at the outputBuffer, and if there are any characters in it, invoke a runnable on the UI thread that pulls off the first character and plops it into the textView.  The thread then sleeps for about 100ms. (There are some shenanigans here... while the teletype was amazing when I used it in 1979, it's a little slow for my tastes now.  So every 10 characters, I reduce the delay slightly.  When there are no more characters to display, I reset the delay to 100ms...)</p>

<p>I edited off the bottom of the class since it was not germane to my question.</p>

<p>What I have here seems to work well.  However, does it work because of me or in spite of me?  What are your preferred ways of writing Threads and Handlers?</p>

<pre><code>public class MyActivity extends Activity {
    private TextView textView;
    private ScrollView scrollView;
    private StringBuilder outputBuffer;
    private Handler handler;
    private CharacterLooperThread characterLooperThread;
</code></pre>

<p>(snip)</p>

<pre><code>   private class CharacterLooperThread extends Thread {
        private boolean allowRun;
        private Runnable run;
        int effectiveCharacterDelay;
        int characterCount;

        public CharacterLooperThread() {
            allowRun = true;

            run = new Runnable() {
                public void run() {
                    /**
                     * Don't do anything if the string has been consumed. This is necessary since when the delay
                     * is very small it is possible for a runnable to be queued before the previous runnable has
                     * consumed the final character from the outputBuffer. The 2nd runnable will cause an
                     * exception on the substring() below.
                     */
                    if (outputBuffer.length() == 0) return;

                    try {
                        textView.append(outputBuffer.substring(0, 1));
                        scrollToBottom();
                        outputBuffer.deleteCharAt(0);
                    } catch (Exception e) {
                        toast(getMsg(e));
                    }
                }
            };
        }

        public void run() {
            resetDelay();
            while (allowRun) {

                /**
                 * This if() performs 2 functions:
                 * 1. It prevents us from queuing useless runnables in the handler.  Why use the resources if
                 * there's nothing to display?
                 * 2. It allows us to reset the delay values.  If the outputBuffer is depleted we can reset the
                 * delay to the starting value.
                 */
                if (outputBuffer.length() &gt; 0) {
                    handler.post(run);
                    reduceDelay();
                } else {
                    resetDelay();
                }
                try {
                    Thread.sleep(effectiveCharacterDelay);
                } catch (InterruptedException e) {
                    toast("sleep() failed with " + e.getMessage());
                }
            }
            /**
             * Make sure there's no runnable on the queue when the thread exits.
             */
            handler.removeCallbacks(run);
        }

        public void exit() {
            allowRun = false;
        }
</code></pre>
