Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java暂停进程并使用另一个流执行_Java_Multithreading - Fatal编程技术网

Java暂停进程并使用另一个流执行

Java暂停进程并使用另一个流执行,java,multithreading,Java,Multithreading,我想做这件事,但不知道怎么做?该代码请求网络服务发送短信计费信息;当计费成功或失败时,记录网络服务的响应。 有时需要很长时间才能得到响应,我想取消/暂停该过程,并用新号码重新发送请求 long before = System.currentTimeMillis(); String resp = new SmsConnection().doResponseRequest(sms); long totalResponseTime=((System.currentTimeMillis() - befo

我想做这件事,但不知道怎么做?该代码请求网络服务发送短信计费信息;当计费成功或失败时,记录网络服务的响应。 有时需要很长时间才能得到响应,我想取消/暂停该过程,并用新号码重新发送请求

long before = System.currentTimeMillis();
String resp = new SmsConnection().doResponseRequest(sms);
long totalResponseTime=((System.currentTimeMillis() - before )/1000);
我只能记录
totalResponseTime
,有时需要50-100秒才能从服务获得响应。我能说什么呢

“如果响应时间超过15秒,请取消/暂停此请求,同时重新发送另一个请求。收到响应后,我们将处理该请求。”

我需要一个超时选项来接收响应。请建议

谢谢,

您可以通过提取
新的SmsConnection().doResponseRequest(sms)来使用它进入。您可以看到一个示例。

使用可调用:

Future<String> futureResponse = service.submit(new YourCallable); //service is the ExecutorService that you use
//this will block for 15 seconds here
String result = futureResponse.get(15, TimeUnit.SECONDS); //this needs to wrapped in a try catch with TimeoutException
if(result == null){
    //You did not get the result in 15 seconds
    futureResponse.cancel(true);//kill this task

    //schedule a new task
    service.submit(new YouCallable());

}
Future-futuresponse=service.submit(新的可调用的)//服务是您使用的服务
//这将在这里阻塞15秒
字符串结果=futureResponse.get(15,时间单位为秒)//这需要包装在带有TimeoutException的try-catch中
如果(结果==null){
//你没有在15秒内得到结果
futuresponse.cancel(true);//终止此任务
//安排新任务
提交(新的YouCallable());
}

这是解决我问题的简单方法!感谢尤金和德米特里提供的提示和建议!我创建了一个接收响应的方法,然后在synchronized(this){}块内调用了这个方法

 public String getResponse(final StsSmsSubmit sms) throws InterruptedException, ExecutionException
    {
        String response="";
       Callable<String> responsecode=new Callable<String>() {


        @Override
        public String call() throws Exception {

           final String resp = new StsSmsConnection().doRequest(sms);
           return resp;
        }
    };
       ExecutorService service=Executors.newSingleThreadExecutor();
         Future task=service.submit(responsecode);
         try{
         response=(String) task.get(30, TimeUnit.SECONDS);
    }
    catch(TimeoutException tE)
    {
       System.out.println("TimeOutException Occurred  "+tE.getMessage()+"at "+ new Date());
       log.fatal("TimeOut Exception is catched. at "+ new Date());
       response="TimeOut";
    }
         service.shutdown();


       return response;

    }
公共字符串getResponse(最终StsSmsSubmit sms)引发InterruptedException、ExecutionException
{
字符串响应=”;
Callable responsecode=新的Callable(){
@凌驾
公共字符串调用()引发异常{
final String resp=new StsSmsConnection().doRequest(sms);
返回响应;
}
};
ExecutorService=Executors.newSingleThreadExecutor();
未来任务=服务。提交(响应代码);
试一试{
响应=(字符串)task.get(30,TimeUnit.SECONDS);
}
捕获(TimeoutException tE)
{
System.out.println(“TimeOutException发生在”+tE.getMessage()+”的“+new Date());
致命(“捕获超时异常。在“+新日期()”);
response=“超时”;
}
service.shutdown();
返回响应;
}

是否仅限于线程?为什么不使用它们呢?它们灵活且易于控制!。。谢谢此应用程序已在使用中。如果需要太多时间才能得到响应,我只想取消这部分。@Eugene。。谢谢你的主意@马丹马丹