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

  • SEARCH
  • Home
  • 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 some code like this in a winforms app I was writing to
We've got an app with some legacy printer setup code that we are still
I'd much rather code an app using pure C api such as OpenGL, rather
Our rails app is designed as a single code base linking to multiple client
My app code is starting to get unmanageable and I want to start source
Our iPhone app code currently uses NSURLConnection sendSynchronousRequest and that works fine except we
What should be the general guidelines/gotchas for dividing application code (App_Code) into separate files?
This would be a question for anyone who has code in the App_Code folder
Why is that Web Service files (.asmx) have their code-behind placed in app_code folder
In order to apply a triggered animation to all ToolTip s in my app,

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.