几个带有WAIT的aRFC,如何在回调中同步访问变量?

Mic*_*yer 3 parallel-processing sap abap

我正在使用异步RFC调用在SAP中进行一些并行工作。在这里您可以看到我的伪代码。

* class variable
data: gv_counter type i .

....

method start_tasks .

    do 10 times .

        call function 'my_remote_function'
            starting new task task_identifier
            calling task_finish on end of task .

    enddo .

    wait for asynchronous tasks until gv_counter eq 10 .    

endmethod .

.....

method task_finish .

    gv_counter = gv_counter + 1 .

endmethod .
Run Code Online (Sandbox Code Playgroud)

如您所见,我开始了10个过程,然后等到它们全部完成为止。

我的问题是关于方法task_finish和对全局类变量的访问gv_counter。如何确保对变量的访问gv_counter是同步的?

例如在Java中,我会做类似的事情:

synchronized {
    gv_counter += 1 ;
}
Run Code Online (Sandbox Code Playgroud)

Jag*_*ger 5

Here is a quotation from the SAP documentation on the topic.

Addition 2

... {CALLING meth}|{PERFORMING subr} ON END OF TASK

...

If multiple callback routines are registered during a program section, they are executed in an undefined order when the work process changes in a roll-in.

For me it means that they will be executed one after another (in order) which is however undefined. This would mean that your variable would always reach the value of 10.

You can actually debug it, to see how it is processed sequentially when you put a breakpoint in the task_finish method. Here is my example.

REPORT ZZZ.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main,
      task_finish
        IMPORTING
          p_task TYPE clike.
  PRIVATE SECTION.
    CLASS-DATA:
      gv_counter TYPE i.
    CLASS-METHODS:
      start_tasks.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    start_tasks( ).
  ENDMETHOD.

  METHOD start_tasks.
    DATA: l_task TYPE string.
    DO 10 TIMES.
      l_task = sy-index.
      CALL FUNCTION 'Z_ARFC_ECHO'
        STARTING NEW TASK l_task
        CALLING task_finish ON END OF TASK
        EXPORTING
          i_value       = sy-index.

    ENDDO.

    WAIT FOR ASYNCHRONOUS TASKS UNTIL gv_counter = 10.
  ENDMETHOD.

  METHOD task_finish.
    DATA: l_value TYPE sy-index.
    RECEIVE RESULTS FROM FUNCTION 'Z_ARFC_ECHO'
      IMPORTING
        e_echo = l_value.

    WRITE: /, p_task, l_value.

    gv_counter = gv_counter + 1.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).
Run Code Online (Sandbox Code Playgroud)

My RFC looks as follows

FUNCTION Z_ARFC_ECHO.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(I_VALUE) TYPE  SY-INDEX
*"  EXPORTING
*"     VALUE(E_ECHO) TYPE  SY-INDEX
*"----------------------------------------------------------------------

e_echo = i_value.

ENDFUNCTION.
Run Code Online (Sandbox Code Playgroud)

What is also interesting (and also mentioned in the documentation) the list output statements like WRITE are not processed in such a handler, so that's why you do not see anything getting printed at the end of the execution of the abovementioned report.