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);
}
}