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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:51:15+00:00 2026-06-15T13:51:15+00:00

If the method call takes more than 10 seconds I want to kill it

  • 0

If the method call takes more than 10 seconds I want to kill it and move on.

Is multi-threading my only option to solve this problem?

If I go with multi-threading which I’m very new at, my run method would only contain the one method as follows. If myMethod() gets stuck in an infinite loop is there a way to interrupt this thread?

public void run() {
    myMethod();
}

Let’s assume myMethod() is the following and changing it is not an option.

while(true){
    System.out.println("Thinking.");
        for (int i = 0; i < 100000; i++) {
            //wasting time
        }
}

I was going for Mik378’s solution, but I don’t think this will work as myMethod doesn’t throw the InterruptedException exception.

  • 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-15T13:51:16+00:00Added an answer on June 15, 2026 at 1:51 pm

    Using threads to achieve this is tricky. There is no portable and safe method to kill a thread without cooperation from said thread. For further details, see How do you kill a thread in Java? and Why are Thread.stop [et al] deprecated?

    A more robust approach is to call the method in the context of a separate process, and kill that process.

    An even better approach is to understand why things are getting stuck in the first place, and figure out a way to prevent them from getting stuck (or a way to have a timeout and throw an exception etc). Whether that’s a realistic possibility in your case, I don’t know.

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

Sidebar

Related Questions

In Scala, I can create a method that takes more than one argument list:
I have the below code. What happens if the Upload method takes more than
I have several models that need to call a method that takes a particular
Take this example Proc: proc = Proc.new {|x,y,&block| block.call(x,y,self.instance_method)} It takes two arguments, x
I have this method call I have to use... financial_document.assets.length But financial_document.assets could be
I have this method call: public DataTemplate Create(Type type, string propertyName) { string str
i need to log each method call in my application. i dont want to
Can you intercept a method call in Perl, do something with the arguments, and
Can I intercept a method call in Objective-C? How? Edit: Mark Powell 's answer
I'm attempting to build a method call from strings that have been passed into

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.