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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:36:36+00:00 2026-06-17T08:36:36+00:00

Here’s what I want to do. Given a function public void foo() { }

  • 0

Here’s what I want to do. Given a function

public void foo() {

}

I would like to have it end after certain time has elapsed. That is, imagine this is some kind of random generator which has to produce random objects that satisfy some difficult constraints and hence it may or may not succeed under a given time allotment. That is, the function may actually be something like this

public void foo() {
   //task1
   while(fails) {
     //...
   }

   //task2
   while(fails2) {
      //...
   }

   //more tasks may follow, which use the data from the previous tasks to further try to satisfy difficult conditions
}

That is simply just an example. But the point is that the function consists of many while loops, many test cases, and lots of heavy computation.

The goal: I want to be able to say “run foo() and if 4 seconds has elapsed and foo() is still not done, then stop foo() immediately.”

What I have tried: I have tried to include conditions on just about every line of foo() to see how much time has elapsed and to return out of the function if the 4 seconds has passed. But given how complicated foo() is, this is clearly very difficult to do code wise because this requires testing the time on every single line of the function.

My thought logic: I think this should be possible because there are functions that do this sort of thing, that terminate code regardless of the state, such as System.exit(1). That is the idea. I’d like to be able to call, from the outside, to have this function foo() terminate.

  • 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-17T08:36:37+00:00Added an answer on June 17, 2026 at 8:36 am
    // foo method and global variables used
    private static ArrayList<Integer> foo() {
        // info class
        class Info {
            public boolean run, completed;
            public ArrayList<Integer> list;
        }
        // declare info object, list
        final Info info = new Info();
        final Object wait = new Object();
        // run a new thread
        Thread t = new Thread(
            new Runnable() {
                // run method
                @Override
                public void run() {
                    // setup run
                    info.run = true;
                    info.completed = false;
                    info.list = new ArrayList<>();
                    // loop to modify list. Don't put a big piece of code that will
                    // take a long time to execute in here. 
                    while(info.run) {
                        // example of what you should be doing in here:
                        info.list.add(1);
                        // and if you are done modifying the list, use:
                        break;
                    }
                    // done modifying list
                    info.completed = true;
                    synchronized(wait) {
                        wait.notify();
                    }
                }
            }
        );
        t.start();
        // wait for four seconds, then return list
        try {
            synchronized(wait) {
                wait.wait(4000);
            }
        } catch (InterruptedException e) { e.printStackTrace(); }
        info.run = false;
        return info.completed ? info.list : null;
    }
    // main method
    public static void main(String[] args) {
        // get list
        ArrayList<Integer> list = foo();
        System.out.println("Done!");
    }
    

    What the foo() method does?

    1. Begins to modify the list it will eventually return
    2. If the time took modifying this list exceeds four seconds, it will stop modifying the list and return the list.
    3. It will return null if the list was stopped early.
    4. It now only uses local variables!
    5. Nice bonus, it will immediately return the list the second modifying it is done.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the situation, i want to have a user that can enter time on
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is my unit test code: [TestMethod] public void GetMyAttachmentTest() { var files =
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is what I want to do. Use this HTML line and have the
Here's my situation. I have a DotNetNuke application. I want to link to an
Here is a scenario: User installs .NET application that you have made. After some
Here's what I'd like to do: have a banner across the top of a
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }

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.