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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:46:15+00:00 2026-06-02T17:46:15+00:00

Alongside the implicit User Interface thread, i have made two threads (runnables), both having

  • 0

Alongside the implicit User Interface thread, i have made two threads (runnables), both having a while loop inside them in which i periodically check for updates inside a message queue which I’ve implemented.

The problem at first was that the two while loops were infinite, and they changed so quickly and so much that they used up almost all the CPU of the device.
So i thought about making the while loops sleep for about 100 milisec after each cycle, to let the other threads do their work, but i came across another issue:

NOW, the problem is that the threads sleep for 100 milliseconds, but they don’t let the UI thread work during that time 🙁 And what this does is make the UI laggy. When i drag something on the screen it lags a bit.

So what i want to do is make these threads (these while loops) check the message queue flag every 100 miliseconds, but during those 100 miliseconds i want to let the UI thread run. How can this be accomplished?

EDIT 1

I found the yield method, which stops the running of the current thread, but when do i call this? I would need to call the sleep thread and at the same time make it yield somehow … ??

EDIT 2

The two classes are singletons, and i start them from the first Activity of the application, so when the app starts (the UI thread)

//Obtain object of singleton classes
ControllerSingleton controller = ControllerSingleton.getControllerSingleton();
OpponentSingleton opponent = OpponentSingleton.getOpponentSingleton();

//Start threads
/*--------CONTROLLER----------*/
Thread t;
t = new Thread( controller );       
t.setName("ControllerThread");
Log.i("Remy","Starting Controller thread...");
t.start();

/*--------OPPONENTS----------*/
t = new Thread( opponent );     
t.setName("OpponentThread");
Log.i("Remy","Starting Opponent thread...");
t.start();

And inside each run() i have the next code:

public void run() 
{
   while( true )
   {
      //Check if any messages in the queue
      if ( queueEmpty() == false )
      {
          //Do something ...              
      }
      /*HERE IS WHERE I WOULD LIKE TO TELL THE THREAD TO STOP AND WAIT FOR 100 ms*/
   }
}
  • 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-06-02T17:46:16+00:00Added an answer on June 2, 2026 at 5:46 pm

    I think that the thing you need to use is Handler. You might want to see this tutorial for example.

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

Sidebar

Related Questions

I have a hird-party dll that need to be installed alongside with my app.
I'm having some trouble floating images properly alongside text in a contenteditable div. Here's
Using stomp.py (3.0.5) with python (2.6) alongside Apache ActiveMQ (5.5.1). I have got the
I am using curses alongside the logging module and I'm having a problem with
I have an array (for checkboxes) that I need to pass alongside the regular
I have been working alongside The Little Schemer to learn Scheme and using PLT-Scheme
I have setup a simple Oracle external table test that I (alongside a DBA
I have a button on a page alongside some JS which gives it click-and-hold
I currently have Ubuntu 10.04 installed alongside windows 7 and would like to uninstall
If I run svnserve.exe alongside Subversion Edge, would it cause any conflicts? We have

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.