Java同步方法依次执行,具体取决于方法响应

时间:2016-04-28 15:16:50

标签: 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 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");
}

我不确定是否为此方案使用synchronize或asynchronize。

我更关心executeTicketCommand()的方法执行,因为目前,即使先前的executeTicketCommand()尚未完成,也会调用它们。

我收到来自后来的executeTicketCommand()执行的响应,我应该等待当前的票务流程完成,然后才开始处理下一个命令。这类似于在处理过程中锁定操作,在处理之后......它可以再次运行或调用。

此示例只是简化问题代码的表示,因为实际代码非常复杂。

0 个答案:

没有答案