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 6941299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:52:03+00:00 2026-05-27T12:52:03+00:00

I have an app that runs 2 threads in loops. 1st one is updating

  • 0

I have an app that runs 2 threads in loops. 1st one is updating a graph in 1s interval and the second one is updating another graph at 60s interval. The second task is taking a long time since it is quering some server in the internet 3 times that might not always be available and even if it is it will take up to 5-7s to execute.

What is happening is when I launch the second thread it will pause execution of the first one and that is not what I want, I wish both run concurrently. Here in the Youtube video you can see the results of the app running. http://youtu.be/l7K5zSWzlxI
“thread_updater1s” is running a green graph, large readout, and a timer in the corner so you clearly see it stalls for 11 seconds.

1)First of all why is that happening? how to fix it?

2)I’m aware that I might not launch the threads properly at all. I had hard time understanding how to make something to run in a interval loop in Java and my code worked fine for one graph/tread. Now when I have 2 loops in separate threads I don’t know why they are not executing concurrently.

Here is the code:

public class LoopExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    thread_updater1s.start();
    thread_updater2.start();
}// end of onCreate

final Runnable r1s = new Runnable() {
    public void run() {
        do_1s_updates(); // those are very quick http calls to the local API server 
    }                    // to get data nessessary for some plot.
                        // They have 1s timeout as well but rarely timeout
};

final Runnable r2 = new Runnable() {
    public void run() {
        do_large_updates(); //This makes 7 long call over the Internet to the slow https
                            //server once every 60s. Has 10s timeout and sometimes takes as much as
                            //7s to execute
        }
};

Thread thread_updater1s = new Thread() {
    @Override
    public void run() {
        try {
            while (true) {
                handler.post(r1s);
                sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};

Thread thread_updater2 = new Thread() {
    @Override
    public void run() {
        try {
            while (true) {
                handler2.post(r2);
                sleep(60000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};

}

PS. please be forgiving and informative I only code Java for 15 days so far with absolutely no prior experince or lesson.

  • 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-27T12:52:04+00:00Added an answer on May 27, 2026 at 12:52 pm

    You need to make the http requests in the threads (not the posted runnables). Then, when you have the data downloaded, you create a runnable with that data that will update the graph and post that runnable to be executed by the UI thread. Here is an example:

    public class LoopExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        thread_updater1s.start();
        thread_updater2.start();
    }// end of onCreate
    
    Thread thread_updater1s = new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    final Object data = getDataFromServer1();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            updateGraph1(data);
                        }
                    );
                    sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    
    Thread thread_updater2 = new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    final Object data = getDataFromServer2();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            updateGraph2(data);
                        }
                    );
                    sleep(60000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    

    Obviously, change that final Object data by the appropriate class that represents your data downloaded.

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

Sidebar

Related Questions

I have a multi-threaded batch processing app that runs anywhere between 5-10 concurrent threads
I have an app that runs a lot of threads. It was my first
In my Android app, I have a class that extends Thread that runs when
I have an app that runs a stored procedure in SQL Server, checking the
I have an app that runs a webview as a service so the audio
i have an app that runs perfectly on android 1.6+, but admob ads are
I have a web app that runs fine in Visual web developer. But when
I have a Cordova app that runs the jQuery mobile framework and loads pages
I have a windows app that runs correctly in my PC that is 96DPI
I have a web app that runs perfectly on Apache Tomcat. However, when I

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.