在java中,如果响应未返回,如何跳过循环
我的问题是说我的方法在java中,如果响应未返回,如何跳过循环,java,Java,我的问题是说我的方法getAllVendorDetails()将调用另一个方法getVendordetailsById(int-id),如果该方法getVendordetailsById()在指定的时间内(比如30秒)没有返回响应/应答,我需要移动到下一个供应商或跳过执行并向用户显示错误消息。我们如何在Java中实现这个结果。我尝试了下面的代码,但它不工作。在它进入getVendordetailsById()之后,它不会返回 公开课考试{ public static void main(Stri
getAllVendorDetails()
将调用另一个方法getVendordetailsById(int-id)
,如果该方法getVendordetailsById()
在指定的时间内(比如30秒)没有返回响应/应答,我需要移动到下一个供应商或跳过执行并向用户显示错误消息。我们如何在Java中实现这个结果。我尝试了下面的代码,但它不工作。在它进入getVendordetailsById()
之后,它不会返回
公开课考试{
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);
}
publicstaticvoidmain(字符串[]args){
长currentTime=System.currentTimeMillis();
日期日期=新日期(当前时间);
date.setSeconds(date.getSeconds()+30);
long end=date.getTime();
System.out.println(“Time=“+(end currentTime));
List vendorId=new ArrayList();
对于(int i=1;i System.currentTimeMillis()){
国际单项体育联合会(旗){
flag=false;
对于(int i=0;iSystem.currentTimeMillis();i++){
getVendordetailsById(vendorId.get(i));
isSuccess=true;
}
}
System.out.println(“再见”);
}
如果(isSuccess){
System.out.println(“成功”);
}否则{
系统输出打印项次(“错误”);
}
System.out.println(“当前时间=“+System.currentTimeMillis());
}
私有静态void getVendordetailsById(整数id){
System.out.println(“供应商编号=”+id);
int a=0;
对于(inti=1;i,您可以使用java.util.concurrent.Future来解决您的问题
首先,您需要使用java.util.concurrent.Executors创建一个executor来提交未来的作业
比如
//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
}
//建议在循环之外使用它
//或者在静态场中
java.util.concurrent.Executors=java.util.concurrent.Executors.newFixedThreadPool(1);
//…在循环中
Future=es.submit(新可调用{
公共对象调用(){
getVendordetailsById
}
});
试一试{
对象结果=future.get(30,TimeUnit.SECONDS);
}缓存(TimeoutException te){
打破
}缓存(异常te){
//其他异常处理程序
}
您不能从外部返回方法。如果您想要一些超时逻辑,您必须在getVendordetailsById方法中使用,也许?您对getVendordetailsById
方法有任何控制权吗?例如,您可以使该方法无阻塞(或至少“有限阻塞”)或者看看这个问题:,也就是Alex几年前的回答,谈到了FutureTasks和Executor Shi SeKa,我尝试了FutureTasks和Executors选项,它对我有用,谢谢。