Using Handler or Thread with while true?
<p>I am wondered what should I use if I want a task to happen every five seconds when a flag is true. I am running it on an andrid device, so the performance is important.</p>

<p>Option one is with an Handler:</p>

<pre><code>public void handleLocation() {
    handler.postDelayed(new Runnable() {
        public void run() {
            Toast.makeText(mContext, "Five Seconds", Toast.LENGTH_SHORT).show();          // this method will contain your almost-finished HTTP calls
            if (currentLocation != null &amp;&amp; isWorking) {
                setMockLocation(currentLocation);
                setMockLocation2(currentLocation);
            }
            handler.postDelayed(this, FIVE_SECONDS);
        }
    }, FIVE_SECONDS);
}
</code></pre>

<p>Second option is with a Thread:</p>

<pre><code> public void run() {
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    if (isWorking) {
                        if (currentLocation != null)
                            setMockLocation(currentLocation);
                        setMockLocation2(currentLocation);
                    }
                    sleep(5000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                Toast.makeText(mContext, mContext.getString(R.string.err0_unknown), Toast.LENGTH_LONG).show();
            }
        }
    };

    thread.start();
}
</code></pre>

<p>What do you prefer to use? Is there a better solution?</p>