Right. A Thread is a Runnable but treating it like a Runnable may not
launch it on a separate Thread. I'll re-organize this around a single
Thread.

Thanks for the code review and suggestions. Once again proving that
automated testing and code review leads to better quality code.

--
Jeremy Wadsack

On Oct 7, 2:38 pm, DanH <[email protected]> wrote:
> But I don't see anywhere in the Handler spec where it says the thread
> will be dispatched.  It appears to me that when the thread is posted,
> Handler will just run its "run" method, without starting the thread.
> If you wanted to run on a separate thread it appears to me that you'd
> have to start the thread, have that thread create a Handler and pass
> it back to you somehow, and then post via THAT Handler.
>
> At least that's how I'd interpret this:  "Each Handler instance is
> associated with a single thread and that thread's message queue. When
> you create a new Handler, it is bound to the thread / message queue of
> the thread that is creating it -- from that point on, it will deliver
> messages and runnables to that message queue and execute them as they
> come out of the message queue."
>
> On Oct 7, 4:25 pm, Jeremy Wadsack <[email protected]> wrote:
>
>
>
> > Fair point. The "// Do some tasks" is doing long-running (Internet-
> > connected) stuff, so I want it to run in a separate thread. As I
> > understand, posting the Runnable without a thread would run it on the
> > main thread.
>
> > I assume the gc will clean up the Threads as they expire, but I could
> > also redesign this to have a single active thread that posts Runnables
> > (or even just messages) to it's own MessageQueue at specified
> > intervals. Then I'd be managing the looper myself (that is, calling
> > Looper.loop), which would probably resolve this issue.
>
> > That doesn't answer the original question but it may be the right
> > approach if it's more "android-y".
>
> > --
> > Jeremy Wadsack
>
> > On Oct 7, 1:20 pm, DanH <[email protected]> wrote:
>
> > > Kind of off-topic, but why are you creating a new Thread with each
> > > post, vs simply posting the Runnable?
>
> > > On Oct 5, 5:47 pm, Jeremy Wadsack <[email protected]> wrote:
>
> > > > I have a class that uses a Handler for a timed, asynchronous activity.
> > > > Something like this:
>
> > > > public class SampleClass {
> > > >   private static final long DELAY = 30000;
> > > >   private boolean isRunning = false;
> > > >   private Handler handler = new Handler();
>
> > > >   public start() {
> > > >     if (!isRunning) {
> > > >       isRunning = true;
> > > >       handler.post(new Thread(task));
> > > >     }
> > > >   }
>
> > > >   public stop() {
> > > >     isRunning = false;
> > > >   }
>
> > > >   private Runnable task = new Runnable() {
> > > >     public void run() {
> > > >       if (!isRunning) {
> > > >         return;
> > > >       }
>
> > > >       // Do some tasks
> > > >       handler.postDelayed(new Thread(this), DELAY);
> > > >     }
> > > >   }
>
> > > > }
>
> > > > I am trying to write a unit test (without having to implement an
> > > > activity that instantiates the class) but I can't seem to get the
> > > > items that are posted to the MessageQueue to ever be fired. Inheriting
> > > > from junit.framework.TestCase doesn't work, but then there wouldn't be
> > > > a MessageQueue for the handler, I'd expect (although, there's no
> > > > error, but the Runnable never gets called). I tried inheriting the
> > > > test class from AndroidTestCase and ApplicationTestCase<Application>
> > > > but neither of those works, even though the former is supposed to
> > > > provide a Context and the latter "all the life cycle of an
> > > > application."
>
> > > > Anyone have any pointers?
>
> > > > --
> > > > Jeremy Wadsack

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to