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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:42+00:00 2026-05-13T11:04:42+00:00

I have a bunch of procedures that need to be executed successively until either

  • 0

I have a bunch of procedures that need to be executed successively until either they are all executed, or a certain condition is met. Here’s the basic code that needs to be executed until a condition is met:

public boolean search()
{
    robot.go();

    robot.spin();

    //etc - around 8 more similar commands (each takes around 2 seconds)
    return false; //didn't find what it was looking for
}

So far, the only way that I’ve thought of doing what I wanted is this:

public boolean search()
{
    robot.go(false);
    while(robot.isMoving())
    {
        if(thingFound())
        {
            robot.stop()
            return true;
        }
        Thread.yield();
    }

    robot.spin(false);
    while(robot.isMoving())
    {
        if(thingFound())
        {
            robot.stop()
            return true;
        }
        Thread.yield();
    }


    //etc - around 8 more similar commands
    return false; //didn't find what it was looking for
}

The false parameter to go() and spin() indicates that they should return immediately, allowing the condition to be checked. However, this approach strikes me as rather inefficient, as the same block of code must be repeated 10 times. Could this be achieved more efficiently with exceptions or concurrent Threads?

  • 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-13T11:04:43+00:00Added an answer on May 13, 2026 at 11:04 am

    based on Jonathan Leffler’s answer:
    you can use a Runnable as pointer to the commands

    private final Runnable going = new Runnable() {
        @Override
        public void run() {
            robot.go(false);
        }
    });
    
    private final Runnable spinning = new Runnable {
        @Override
        public void run() {
            robot.spin(false);
        }
    });
    
    // other commands 
    
    
    private boolean isFoundAfter(Runnable command)
    {
        command.run();
        while (robot.isMoving())
        {
            if (thingFound())
            {
                robot.stop()
                return true;
            }
            Thread.yield();
        }
        return false;
    }
    
    public boolean search()
    {
        if (isFoundAfter(going)) return true;
    
        if (isFoundAfter(spinning)) return true;   
    
        //etc - around 8 more similar commands
        return false; //didn't find what it was looking for
    }
    

    one further step, if appropriate, put the commands in an array or a List and execute it as a script

    ...
    
    private boolean executeSearch(Runnable... commands)
    {
        for (Runnable cmd : commands) {
            if (isFoundAfter(cmd)) return true;
        }
        return false; //didn't find what it was looking for
    }
    
    public boolean search() {
        return executeSearch(going, spinning /* around 8 more similar commands */);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of matches that I need to make, and they all
I have a bunch of seperate little select queries that I need to put
Okay. I have a database that has bunch of stored procedures and references another
I have a bunch of html files that come with my app and are
I have a bunch of code in a routine that looks a bit like
I have a bunch of strings that are dependent on static dictionaries and each
I have a bunch of buttons that have a tapGestureRecognizer linked to them, and
I have 7 different stored procedures that take in a number of parameters and
I currently have a service setup that emails a bunch of files. What I
I have a bunch of stored procedures which use RAISERROR to return to the

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.