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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:58:54+00:00 2026-05-28T07:58:54+00:00

In a java class I have a method that sometimes takes a long time

  • 0

In a java class I have a method that sometimes takes a long time for execution. Maybe it hangs in that method flow. What I want is if the method doesn’t complete in specific time, the program should exit from that method and continue with the rest of flow.

Please let me know is there any way to handle this situation.

  • 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-28T07:58:54+00:00Added an answer on May 28, 2026 at 7:58 am

    You must use threads in order to achieve this. Threads are not harmful 🙂 Example below run a piece of code for 10 seconds and then ends it.

    public class Test {
        public static void main(String args[])
            throws InterruptedException {
    
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("0");
                    method();
                }
            });
            thread.start();
            long endTimeMillis = System.currentTimeMillis() + 10000;
            while (thread.isAlive()) {
                if (System.currentTimeMillis() > endTimeMillis) {
                    System.out.println("1");
                    break;
                }
                try {
                    System.out.println("2");
                    Thread.sleep(500);
                }
                catch (InterruptedException t) {}
            }
    
    
        }
    
        static void method() {
            long endTimeMillis = System.currentTimeMillis() + 10000;
            while (true) {
                // method logic
                System.out.println("3");
                if (System.currentTimeMillis() > endTimeMillis) {
                    // do some clean-up
                    System.out.println("4");
                    return;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
Suppose that I have a Java class with a static method, like so: class
I have a simple web service method that returns a simple java class as
I have a simple Java class that has some methods: public class Utils {
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
I have a java class: it.eng.ancona.view.RuoliView$TabElaborazioneFattureValidazione$ElencoDettaglioElaborazioneFattureValidazione$RigaElencoDettaglioElaborazioneFattureValidazione It's so long for multiple inner class. If
I have an abstract Java class MyAbstractClass with a private method. There is a
I have java class with a method which gets an image from a website:
In Java, the java.util.Arrays class have several static toString(...) methods that take an array
How do Java programmers deal with long method chains? Sometimes one or two methods

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.