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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:21:30+00:00 2026-06-01T22:21:30+00:00

I have a thread in an Android application that I would like to wait

  • 0

I have a thread in an Android application that I would like to wait at a certain point in its operation for either:

  1. A notification from another thread, or
  2. A timeout to occur, which I would like to specify in real time.

Presently I have (roughly) the following:

...
doStuff();
synchronized(this) {
  if (!notificationReceived) wait(TIMEOUTPERIOD);
}
doMoreStuff();
...

and in a method called from another thread:

synchronized(this) {
  notificationReceived = true;
  notifyAll();
}

This almost works. In fact, it worked all the way through debugging and testing, and only stopped working when I tried to deploy the application in the field. Because it turns out that the timer in Object.wait() stops when the phone CPU goes to sleep. As does the one in Thread.sleep(). Oops.

Now, I don’t mind that the call won’t return while the phone is asleep (actually I consider this desirable — the results of the action aren’t needed at any point when the phone is asleep), but if the phone sleeps for the duration of my timeout period, I’d like the action to trigger as soon as it does wake up.

Is there any way I’m missing to do this? I know I could use AlarmManager to send me a broadcast Intent after the required time, and put the continuation code in a (what…? A BroadcastReceiver? I’ve never done this before…) but this would substantially complicate the logic of my application. So: is there an easier way I can put a (cancellable) realtime delay in my thread?

  • 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-01T22:21:32+00:00Added an answer on June 1, 2026 at 10:21 pm

    I solved this problem by registering a BroadcastReceiver dynamically that has a reference to the object I wanted notifying, and making it call notifyAll() when it received a broadcast. I then used AlarmManager to set up alarms for it prior to each call to wait(), and cancelled the alarms after wait() returned if it was not due to a timeout.

    I think there may be a race condition in my code where a timeout occurs at about the same time as an external notification, but in my case I decided it does not matter if I get a duplicated notification from time to time.

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

Sidebar

Related Questions

I have two threads in an Android application, one is the view thread, and
I have a thread that, when its function exits its loop (the exit is
I have an Android application that has a need to perform work in the
I have a requirement to play YouTube video content from within an Android application
Right now I have an application I built that is built for android 10.1
I would like to start a Thread when the Android started, so I made
We currently have an application that sends out SMS to users, it acts like
Here's the deal: I have an Android application that needs to call a web
i have a question. I'm developing an Android Application. Actually, i have a thread
I have a background thread that is sending data about the application's current status

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.