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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:05:39+00:00 2026-06-16T23:05:39+00:00

I have code such that: while(isResponseArrived) Thread.yield(); But what I’d really like to do

  • 0

I have code such that:

while(isResponseArrived)
   Thread.yield();

But what I’d really like to do is something like this:

long startTime = System.currentTimeInMilliseconds();

while(isResponseArrived)
{
   if(isTimeoutReached(startTime))
       throw new TimeOutExcepton(); 
   Thread.yield();
}

I’m not yet sure about throwing an exception or not (it’s not important for this question), but what I’d like to know is how to make it as performant as possible, so I’m not chugging away on the processor. In other words how can I make isTimeoutReached(long startTime) as performance friendly as possible.

I tested:

for(int x=0; x<99999999; x++)
    System.nanoTime();

versus

for(int x=0; x<99999999; x++)
    System.currentTimeInMilliseconds();

And the difference was minimal, less than 10% in terms of time to complete

I also look at using Thread.sleep(), but I really want the user to be notified as quickly as possible if there’s an update and the processor is just waiting. Thread.yield() doesn’t get the processor churning, it’s just a NOP, giving anyone else processor priority, until it’s good to go.

Anyways, what’s the best way to test for a timeout without throttling the CPU? Is this the right method?

  • 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-16T23:05:40+00:00Added an answer on June 16, 2026 at 11:05 pm

    I think it would be more efficient to use wait / notify

    boolean arrived;
    
    public synchronized void waitForResponse(long timeout) throws InterruptedException, TimeoutException {
        long t0 = System.currentTimeMillis() + timeout;
        while (!arrived) {
            long delay = System.currentTimeMillis() - t0;
            if (delay < 0) {
                throw new TimeoutException();
            }
            wait(delay);
        }
    }
    
    public synchronized void responseArrived() {
        arrived = true;
        notifyAll();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, i have such-like code that prevent select all action from keyboard: $(document).keydown(function(e){ //
Suppose I have code that maintains a parent/children structure. In such a structure I
Given the following code below, how can I have it modified such that it
I read that Windows wp8 allows native code. So how then have ports such
I have code such as void myfunc() { introduction(); while(condition()) { complex(); loop(); interior();
I have a dilemna that needs I've been thinking a while but still haven't
I have shown the problem at http://jsfiddle.net/7ZpCW/1/ I have implemented the code such that
I have code that is currently 'open source' but not easily accessible since it
I have code that must poll an external resource periodically. Simplified, it looks like:
Problem I have such code var ls = src.iter.toList src.iter = ls.iterator (this is

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.