Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6600899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:41:01+00:00 2026-05-25T18:41:01+00:00

In my App code I’ve extended the Button view (say it as DigiButton )

  • 0

In my App code I’ve extended the Button view (say it as DigiButton ) and overridden the onDraw() method. In onDraw() method I am setting the button text to some counter value. This counter value is decremented by 1 for every 500 ms by the Handler object (I am using Handler.run() method to update the counter value).

Now I’ve created two instances of “DigiButton” with different initial values of counter in “DigiComponent” class. “DigiComponent” has a button whose onClick event starts running both the threads.

I see some delay in the counter value update(on both the instances). I thought that each of the thread is interfering with the other thread execution. To confirm this I just started running only one “DigiButton” instance. Then the counter value got updated correct. Below is my code:

   class DigiButton extends Button{

        private int counter;

        private Runnable handleRunnable;

        DigiButton(Context context,int val){
          super(context);
          counter = val;
          initialize();
        }

       private void initialize(int val){ 
          digiHandler = new Handler();
          handleRunnable = new Runnable(){
          public void run(){

          }          
       };
       }

      private void updateCounter(){ 
         counter++;
         if(counter>0){
           runCounter();
         }
      }


       public void runCounter(){
            digiHandler.postDelayed(handleRunnable, 500);
       }


        @Override
    protected void onDraw(Canvas canvas){       
        super.onDraw(canvas);               
        setText(counter);
    }


   }



 public class DigiComponent extends Activity implements OnClickListener{
    DigiButton thread1, thread2;

    @Override
    public void onCreate(Bundle savedInstanceState) {            
        super.onCreate(savedInstanceState);
        thread1 = new DigiButton(this,30000);
        thread2 = new DigitButton(this,20000);
        Button test = new Button(this);
        LinearLayout layout = new LinearLayout(this);
        layout.addView(thread1);
        layout.addView(thread2);
        layout.addView(test);
        test.setClickListener(this);
        setContentView(layout);
    }


   public void onClick(View v){
     thread1.runCounter();
     thread2.runCounter();
   }
 }

Is that my guess of thread execution interference is correct? If so how to avoid this? Is it something wrong with my thread handling code?

Note: This delay in counter value update is worse when I press “Home” button to go out of the App and I open the App again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:41:02+00:00Added an answer on May 25, 2026 at 6:41 pm

    These are not separate threads. These are Runnables scheduled to run on the main UI thread. It gets worse when you press home because you are adding more and more Runnables to the queue (that reschedule themselves).

    You should look into AsyncTasks with the use of AsyncTask.publishProgress or just use a basic Thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have traced a problem in my iPhone app code to the viewDidAppear method
I've built one variable (horaires_lundi) in this file : app/code/community/Unirgy/StoreLocator/controllers/Adminhtml/Locationcontroller.php ->setTitle($this->getRequest()->getParam('title')) ->setNotes($this->getRequest()->getParam('notes')) ->setStreetAddress($this->getRequest()->getParam('street_address')) ->setHorairesLundi($this->getRequest()->getParam('horaires_lundi'))
I have a wcf service application that has some application startup code in the
Looking at creating a simple batch file for my app. My app needs some
I've got some code to try and loop through LINQ results, but it doesn't
I have a app code that sends in request only when the app opens.
I would like to know the best way to profile app code on OS
I am looking to obfuscate our Java web app code within our existing Ant
I was wondering if there was a way within an app code to determine
Our iPhone app code currently uses NSURLConnection sendSynchronousRequest and that works fine except we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.