2

My problem is say my method getAllVendorDetails() will call another method getVendordetailsById(int id), if that method getVendordetailsById() not returned response/answer within the specified time (like 30secs), I need to move to next vendor or skip the execution and show error message to the user. how can we achieve this result in Java. I tried by below code, but its not working. After it went inside the getVendordetailsById(), its not returned.

public class Test {

public static void main(String[] args) {
    long currentTime=System.currentTimeMillis();
    Date date=new Date(currentTime);
    date.setSeconds(date.getSeconds()+30);
    long end=date.getTime();
    System.out.println("Time="+(end-currentTime));

    List<Integer> vendorId= new ArrayList<Integer>();
    for (int i = 1; i <= 100000; i++) {
        vendorId.add(i);
    }
    boolean isSuccess=false;
    boolean flag=true;

    while(end>System.currentTimeMillis()){

        if(flag){
            flag=false;
            for (int i = 0; i < vendorId.size() && end>System.currentTimeMillis() ; i++) {
                getVendordetailsById(vendorId.get(i));
                isSuccess=true;
            }
        }
        System.out.println("bye");
    }

    if(isSuccess){
        System.out.println("Success");
    }else{
        System.out.println("Errorr");
    }



    System.out.println("Current time="+System.currentTimeMillis());
}

private static void getVendordetailsById(Integer id) {
    System.out.println("vendor number="+id);
    int a=0;
    for (int i = 1; i <= 1000; i++) {
        a=a+i;
    }

    System.out.println("Current value of a="+a);
}

}

The Cat
  • 2,375
  • 6
  • 25
  • 37
Rajkumar
  • 122
  • 7
  • you cant return method from outside. if you want some timeout logic, you must have do that in **getVendordetailsById** method – Ankit Mar 28 '13 at 11:36
  • 3
    Use [Threading](http://docs.oracle.com/javase/tutorial/essential/concurrency/), perhaps? – MD Sayem Ahmed Mar 28 '13 at 11:36
  • Do you have any control over the `getVendordetailsById` method? Like can you make that method non-blocking (or at least "limited blocking")? – SeKa Mar 28 '13 at 11:48
  • Or check out this SO question: http://stackoverflow.com/questions/240320/how-can-i-wrap-a-method-so-that-i-can-kill-its-execution-if-it-exceeds-a-specifi, namely an answer by Alex a few down that talks about FutureTasks and Executors – SeKa Mar 28 '13 at 12:07
  • Hi SeKa, I tried the FutureTasks & Executors options, it works for me, thanks. – Rajkumar Mar 28 '13 at 13:00

1 Answers1

0

You can use the java.util.concurrent.Future to solve your problem.

First, you need to use java.util.concurrent.Executors to created a executor to submit your future job

such as

   //suggest that use it out of the loop
   //or in the static field
   java.util.concurrent.ExecutorService es = java.util.concurrent.Executors.newFixedThreadPool(1);
   //...in the loop
   Future<Object> future = es.submit(new Callable<Object>{
       public Object call(){
           getVendordetailsById 
       }
   });
   try{
      Object result = future.get(30, TimeUnit.SECONDS);
   }cache(TimeoutException te){
     break;
   }cache(Exception te){
      //other exception handler
   }
jiacheo
  • 308
  • 1
  • 2
  • 12