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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:24:15+00:00 2026-06-09T18:24:15+00:00

Right now, I have a piece of code that contacts another server asking if

  • 0

Right now, I have a piece of code that contacts another server asking if an item is in a list, and returns a boolean value based on that returned value.

The code goes like so:

public boolean checkIfOnline(int accountId) {
    //First loop is incase if someone is already checking. Second is for the checking that this account is doing.
    while (isCheckingIfOnline) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException ex) {
        }
    }
    isCheckingIfOnline = true;
    sendCheckIfOnline(accountId);
    while (isCheckingIfOnline) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException ex) {
        }
    }
    return onlineResponse;
}

The onlineResponse and isCheckingIfOnline are changed within a method that handles what the other server returns, and this is the method I’ve thrown together to have the system wait for the other server to respond. Obviously, this is very flawed, as when this method gets called often, it’ll slow down the system since it only allows for one query at a time, when it should allow for multiple queries to be executed simultaneously.

What other method could I use that accomplishes what the above code does, but allows for more than one query to run at once?

Edit: To clarify even more, checkIfOnline takes an account ID, and asks another server is that account ID is on a list, which that other server responds to the current server if the account ID is or is not on the list.

  • 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-09T18:24:16+00:00Added an answer on June 9, 2026 at 6:24 pm

    Sounds like you would want to make use of the ExecutorService in Java 6+.

    The ExecutorService requires you to submit to it a class that implements Callable. When you submit a Callable to a ES, you receive back a Future that you can use to do a number of things, including cancelling the process or getting a result from a completed process.

    It’s a little hard for me to understand exactly what you are trying to achieve with your code and why you’re threading that particular part. That being said, if you want to achieve concurrency there, you’d have to:

    1. submit a Callable to the ES that does the online checks & query;
    2. provide a way for the Callable to notify the your application that it has finished it’s execution.

    It will not be sufficient to simply submit the task and call Future.get() on it because whatever thread makes that call will be suspended until the task is completed.

    You’d need to either allow the Callable to invoke a callback, or thread the class that performs the submission of the task and allow it to sit and wait for the future.get() method to return a result.

    Good luck 🙂

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

Sidebar

Related Questions

So right now I have a program containing a piece of code that looks
I have a piece of javascript code like this right now. document.getElementById(pos).style.backgroundColor='#800080'; I want
I have a small piece of code that produce PrimeFaces dropdown List p:selectOneMenu ,
Right now I have an upload field while uploads files to the server. The
Right now I have a script that will get the last five files in
Right now I have a function, in a class that is used to listen
Right now I have a link that causes a hidden div to appear when
Right now I have a foreach loop that grabs the first link with an
Right now I have a PHP file that does a MYSQL query and then
So I have made a little piece of code that will insert and manipulate

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.