Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to skip the loop if response not return in java
    text
    copied!<p>My problem is say my method <code>getAllVendorDetails()</code> will call another method <code>getVendordetailsById(int id)</code>, if that method <code>getVendordetailsById()</code> 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 <code>getVendordetailsById()</code>, its not returned.</p> <p>public class Test {</p> <pre><code>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&lt;Integer&gt; vendorId= new ArrayList&lt;Integer&gt;(); for (int i = 1; i &lt;= 100000; i++) { vendorId.add(i); } boolean isSuccess=false; boolean flag=true; while(end&gt;System.currentTimeMillis()){ if(flag){ flag=false; for (int i = 0; i &lt; vendorId.size() &amp;&amp; end&gt;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 &lt;= 1000; i++) { a=a+i; } System.out.println("Current value of a="+a); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload