[Last Call] Learn how to a build a cloud-first strategyRegister Now


java method execution time out

Posted on 2012-09-21
Medium Priority
Last Modified: 2012-09-25
Calling a method abc  in xyz class. If this method execution is not completed in 1 min then I need to throw exception and do other stuff in that class.
In abc I am calling  athird party API
Please provide a good solution where I can implement it

Many Thanks.
Question by:vjysakshi
  • 2

Accepted Solution

yats earned 2000 total points
ID: 38424184
You can use java.util.concurrent.ExecutorService and java.util.concurrent.Callable interfaces.
try the below code(timeout in 5 seconds):
public static void main(String[] args) throws InterruptedException {  
          ExecutorService executor = Executors.newCachedThreadPool();
          Callable<Object> task = new Callable<Object>() {
             public Object call(){
                //call method here
          Future<Object> future = executor.submit(task);
          try {
             Object result = future.get(5, TimeUnit.SECONDS);
          } catch (TimeoutException ex) {
          } catch (InterruptedException e) {
          } catch (ExecutionException e) {
          } finally {
                System.out.println("In finally Block");

Expert Comment

ID: 38430407
Is the solution helpful? Waiting for your acknowledgement.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question