Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Handler - Runnable Performance Issues at 40Hz
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
marcpolo  
View profile  
 More options Oct 30 2012, 3:41 pm
From: marcpolo <marc.armstron...@gmail.com>
Date: Tue, 30 Oct 2012 12:41:05 -0700 (PDT)
Local: Tues, Oct 30 2012 3:41 pm
Subject: Handler - Runnable Performance Issues at 40Hz

Hi,

I'm developing an application to perform range measurements at 40Hz and
display them on a UI.

To achieve this I have created a Runnable class to perform each cycle and I
post to the message queue using the postDelay command to schedule the next
command for 25ms. My run method is as follows.

        @Override
        public void run()
        {
            m_handler.postDelayed(this, (int)m_samplePeriod_ms);
            displayRangeData();
        }
My timing measurements have shown that around half of the time the run
method gets executed at 25ms intervals, whereas the rest are typically
around 29-32ms.

Is there a way to increase the priority of the Runnable thread so that I
can achieve a consistent 40Hz?

Am I being unrealistic in my expectations? I am running on a Samsung Galaxy
S3.

Are there other alternatives? I have tried using a thread but I run into
problems when I try to update the UI.

Thanks in advance.

Marc.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bob  
View profile  
 More options Oct 30 2012, 5:28 pm
From: bob <b...@coolfone.comze.com>
Date: Tue, 30 Oct 2012 14:28:39 -0700 (PDT)
Local: Tues, Oct 30 2012 5:28 pm
Subject: Re: Handler - Runnable Performance Issues at 40Hz

Sounds like you want a java.util.Timer


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lew  
View profile  
 More options Oct 30 2012, 5:46 pm
From: Lew <lewbl...@gmail.com>
Date: Tue, 30 Oct 2012 14:46:12 -0700 (PDT)
Local: Tues, Oct 30 2012 5:46 pm
Subject: Re: Handler - Runnable Performance Issues at 40Hz

bob wrote:
> Sounds like you want a java.util.Timer

http://developer.android.com/reference/java/util/Timer.html
"This class does not offer guarantees about the real-time nature of task
scheduling."
"Prefer ScheduledThreadPoolExecutor<http://developer.android.com/reference/java/util/concurrent/Scheduled...> for
new code."

marcpolo, this illustrates why you should take advice with a grain of salt
and a
pound of verification.

'Timer' does not guarantee an exact interval, thus does not solve the OP's
problem.

 marcpolo wrote:

> I'm developing an application to perform range measurements at 40Hz and
> display them on a UI.

> To achieve this I have created a Runnable class to perform each cycle and
> I post to the message queue using the postDelay command to schedule the
> next command for 25ms. My run method is as follows.

>         @Override
>         public void run()
>         {
>             m_handler.postDelayed(this, (int)m_samplePeriod_ms);

Underscores in variable names violate the Java Coding Conventions, with the
exception of constant variables.

>             displayRangeData();
>         }
> My timing measurements have shown that around half of the time the run
> method gets executed at 25ms intervals, whereas the rest are typically
> around 29-32ms.

> Is there a way to increase the priority of the Runnable thread so that I
> can achieve a consistent 40Hz?

> Am I being unrealistic in my expectations? I am running on a Samsung
> Galaxy S3.

> Are there other alternatives? I have tried using a thread but I run into
> problems when I try to update the UI.

What do you mean "tried using a thread"? Show us the code.

You certainly don't want to put that delay on the GUI thread.

Timing in a non-real-time system is approximate.

--
Lew


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
marcpolo  
View profile  
 More options Oct 30 2012, 8:10 pm
From: marcpolo <marc.armstron...@gmail.com>
Date: Tue, 30 Oct 2012 17:10:57 -0700 (PDT)
Local: Tues, Oct 30 2012 8:10 pm
Subject: Re: Handler - Runnable Performance Issues at 40Hz

Thanks very much for the feedback.

When I mentioned that I used a thread, I meant that I extended the Thread
class with the run() method being overriden. I'm at home at the moment so
can't show some code. Basically I would start/stop the thread from a button
'click' handlers. Inside the Run() method was simply a loop to perform the
range measurements and sleep for the remainder of the 25ms. This solution
didn't work because I couldn't update the UI entities from this thread.
Runnable seems to be the better option for accessing views.

I appreciate that android is not a realtime system but thought that 40Hz
wouldn't be too difficult to achieve, with the occasional overrun, which
wouldn't impair my app. I am getting nowhere near this - even when I remove
the range measurement functionality from the run method.

I'll investigate both the timer and scheduledThreadPoolExecutor.

Thanks again.

Marc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kristopher Micinski  
View profile  
 More options Oct 30 2012, 8:16 pm
From: Kristopher Micinski <krismicin...@gmail.com>
Date: Tue, 30 Oct 2012 20:14:46 -0400
Local: Tues, Oct 30 2012 8:14 pm
Subject: Re: [android-developers] Re: Handler - Runnable Performance Issues at 40Hz

On Tue, Oct 30, 2012 at 8:10 PM, marcpolo <marc.armstron...@gmail.com> wrote:
> I appreciate that android is not a realtime system but thought that 40Hz
> wouldn't be too difficult to achieve, with the occasional overrun, which
> wouldn't impair my app. I am getting nowhere near this - even when I remove
> the range measurement functionality from the run method.

It's not that it's "too difficult to achieve," it's just that for most
circumstances it's easier and more efficient to schedule with very
loose timing guarantees.  So this isn't really the framework's fault,
per se, it's more of a "feature" :-)...

kris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bob  
View profile  
 More options Oct 31 2012, 10:16 am
From: bob <b...@coolfone.comze.com>
Date: Wed, 31 Oct 2012 07:16:15 -0700 (PDT)
Local: Wed, Oct 31 2012 10:16 am
Subject: Re: Handler - Runnable Performance Issues at 40Hz

Why not just use post instead of postDelayed?  That's what I use to update
display of the battery level every 10 seconds:

public class Battery_Level_Thread extends Thread {

@Override
public void run() {

while (true)
{
MainActivity.handler.post(new Runnable() {
 @Override
public void run() {
MainActivity.update_battery_level();
 }

});

 try {
Thread.sleep(10*1000);

} catch (InterruptedException e) {

e.printStackTrace();
}

  }
 }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
marcpolo  
View profile  
 More options Oct 31 2012, 12:05 pm
From: marcpolo <marc.armstron...@gmail.com>
Date: Wed, 31 Oct 2012 09:05:03 -0700 (PDT)
Local: Wed, Oct 31 2012 12:05 pm
Subject: Re: Handler - Runnable Performance Issues at 40Hz

I used java.util.Timer and TimerTask instead of the Runnable and this
resulted in much improved performance, allowing me to schedule the task at
40Hz (on average).

As expected, given this is not a RTOS, the cycles vary +/-1-2ms, however I
the the average cycle time is 25ms as desired.

So, it is shown that the Timer performs better than Runnable for scheduling
tasks at frequencies around 40Hz (on the Galaxy S3).

Thanks very much for your help.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kristopher Micinski  
View profile  
 More options Oct 31 2012, 2:26 pm
From: Kristopher Micinski <krismicin...@gmail.com>
Date: Wed, 31 Oct 2012 14:20:18 -0400
Local: Wed, Oct 31 2012 2:20 pm
Subject: Re: [android-developers] Re: Handler - Runnable Performance Issues at 40Hz

It's not an RTOS thing, it's more of a Java thread scheduler thing, so this
happens closer to the framework level.

kris

On Wed, Oct 31, 2012 at 12:05 PM, marcpolo <marc.armstron...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »