Java根据方法响应一个接一个地同步方法执行
我想问一下,如何根据方法的响应在方法上实现顺序执行 例如:Java根据方法响应一个接一个地同步方法执行,java,spring,synchronize,Java,Spring,Synchronize,我想问一下,如何根据方法的响应在方法上实现顺序执行 例如: public void processTicketing() throws TicketingException { /** Process Steps 1-4 could throw TicketingException and does not have exact time of how long each methods will be processed. * ? By default, is the
public void processTicketing() throws TicketingException {
/** Process Steps 1-4 could throw TicketingException and does not have exact time of how long each methods will be processed.
* ? By default, is the sequence of process executions below followed from steps 1 to 4, Depending on how long each methods runs?
* If it is, then disregard this question on processes 1 to 4.
*/
processStep1();
processStep2();
processStep3();
processStep4();
for(int x=0; x < 10; x++){
//each call to this method should wait till the process is finish before calling the next one.
executeTicketCommand();
}
}
private void executeTicketCommand() throws TicketingException {
//NOTE:
//Send Request to a Web Service
//Web Service sends back a response with a status of the process
//The slower the internet, the slower the processing would be
//I want this method to finish first the processing, before being called again inside the calling loop.
if(hasError) throw new TicketingException("Error Processing Ticketing Command");
}
public void processTicketing()引发TicketingException{
/**流程步骤1-4可能引发TicketingException,并且没有处理每个方法的确切时间。
*?默认情况下,根据每个方法的运行时间,是否按照步骤1到步骤4执行以下流程的顺序?
*如果是,则忽略过程1至4中的此问题。
*/
processStep1();
processStep2();
processStep3();
processStep4();
对于(intx=0;x<10;x++){
//每次调用此方法都应该等到进程完成后再调用下一个方法。
executecticketcommand();
}
}
private void executecketcommand()引发TicketingException{
//注:
//向Web服务发送请求
//Web服务发回带有进程状态的响应
//互联网速度越慢,处理速度就越慢
//我希望这个方法首先完成处理,然后在调用循环中再次被调用。
if(hasError)抛出新的TicketingException(“错误处理Ticketing命令”);
}
我不确定在这种情况下是使用同步还是异步
我更关心executecketcommand()的方法执行,因为当前,即使前面的executecketcommand()尚未完成,也会调用它们
我从后面的executecketcommand()执行中得到响应,我应该等待当前票证处理完成,然后再开始处理下一个命令。这类似于在操作仍在处理时锁定操作,在处理之后。。。它可以运行或再次调用
这个示例只是一个简化问题代码的表示,因为实际代码非常复杂 我不明白你的问题,你希望你的代码片段中没有什么?我不太确定我需要在哪里添加关键字synchronized。例如,executecticketcommand()可以在不等待第一次调用完成的情况下同时运行。在调用下一个命令之前,应该首先完成ExecuteCketCommand()。假设ExecuteCketCommand()将返回一个响应OK、警告或引发异常。如果在ExecuteCketCommand()调用期间没有错误,它将继续进行下一次迭代并再次运行。如果需要按顺序执行所有操作,您不需要同步对任何内容的访问,因为它应该由一个线程完成,除非您需要阻止多个线程同时执行您的方法processTicketing。当前,使用给定的代码。它将继续调用executeTicketCommand()十次,即使每个调用尚未完成处理。因此,我担心processStep1()到4可能会继续运行步骤1到4,即使每个进程尚未完成。我不明白您的问题,您希望代码片段中没有什么?我不太确定我需要在哪里添加关键字synchronized。例如,executecticketcommand()可以在不等待第一次调用完成的情况下同时运行。在调用下一个命令之前,应该首先完成ExecuteCketCommand()。假设ExecuteCketCommand()将返回一个响应OK、警告或引发异常。如果在ExecuteCketCommand()调用期间没有错误,它将继续进行下一次迭代并再次运行。如果需要按顺序执行所有操作,您不需要同步对任何内容的访问,因为它应该由一个线程完成,除非您需要阻止多个线程同时执行您的方法processTicketing。当前,使用给定的代码。它将继续调用executeTicketCommand()十次,即使每个调用尚未完成处理。因此,我担心processStep1()到4可能会继续运行步骤1到4,即使每个进程尚未完成。