abap 多线程代码示例
时间: 2023-06-01 17:03:15 浏览: 117
以下是一个 ABAP 多线程代码示例:
REPORT z_multithreading_demo.
TYPES: BEGIN OF ty_result,
value TYPE i,
END OF ty_result.
TYPES: ty_result_tab TYPE STANDARD TABLE OF ty_result WITH EMPTY KEY.
DATA: gt_input TYPE ty_result_tab,
gt_output TYPE ty_result_tab,
gv_threads TYPE i VALUE 5.
DATA: gr_semaphore TYPE REF TO cl_semaphore,
gr_barrier TYPE REF TO cl_barrier.
START-OF-SELECTION.
PERFORM fill_input_tab.
PERFORM create_semaphore.
PERFORM create_barrier.
PERFORM start_threads.
PERFORM print_output_tab.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form fill_input_tab
*&---------------------------------------------------------------------*
FORM fill_input_tab.
DO 100 TIMES.
DATA(ls_input) = VALUE ty_result( value = sy-index ).
APPEND ls_input TO gt_input.
ENDDO.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_semaphore
*&---------------------------------------------------------------------*
FORM create_semaphore.
gr_semaphore = cl_semaphore=>create( initial = gv_threads ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_barrier
*&---------------------------------------------------------------------*
FORM create_barrier.
gr_barrier = cl_barrier=>create( threads = gv_threads ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form start_threads
*&---------------------------------------------------------------------*
FORM start_threads.
DATA: lt_threads TYPE STANDARD TABLE OF REF TO cl_thread WITH EMPTY KEY.
LOOP AT gt_input ASSIGNING FIELD-SYMBOL(<fs_input>).
APPEND VALUE #( REF #( cl_thread=>create( iv_mode = cl_thread=>mode_task
iv_task = NEW lcl_worker( EXPORTING iv_input = <fs_input>
io_output = gt_output ) ) ) ) TO lt_threads.
IF lines( lt_threads ) = gv_threads.
PERFORM run_threads USING lt_threads CHANGING gr_semaphore.
CLEAR lt_threads.
ENDIF.
ENDLOOP.
IF NOT lt_threads IS INITIAL.
PERFORM run_threads USING lt_threads CHANGING gr_semaphore.
ENDIF.
gr_barrier->wait( ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form run_threads
*&---------------------------------------------------------------------*
FORM run_threads USING lt_threads CHANGING cr_semaphore TYPE REF TO cl_semaphore.
DATA(lv_count) = lines( lt_threads ).
DO lv_count TIMES.
DATA(lv_rc) = cr_semaphore->request( ).
IF lv_rc <> 0.
MESSAGE 'Semaphore request failed' TYPE 'E'.
ENDIF.
ENDDO.
DATA: lt_results TYPE ty_result_tab.
LOOP AT lt_threads ASSIGNING FIELD-SYMBOL(<fs_thread>).
<fs_thread>->start( ).
ENDLOOP.
DO lv_count TIMES.
DATA(lv_rc) = cr_semaphore->release( ).
IF lv_rc <> 0.
MESSAGE 'Semaphore release failed' TYPE 'E'.
ENDIF.
ENDDO.
LOOP AT lt_threads ASSIGNING FIELD-SYMBOL(<fs_thread>).
<fs_thread>->join( ).
lt_results = CORRESPONDING #( <fs_thread>->result( ) ) TO lt_results.
ENDLOOP.
gt_output = CORRESPONDING #( lt_results ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form print_output_tab
*&---------------------------------------------------------------------*
FORM print_output_tab.
LOOP AT gt_output ASSIGNING FIELD-SYMBOL(<fs_output>).
WRITE: / <fs_output>-value.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Class lcl_worker DEFINITION
*&---------------------------------------------------------------------*
CLASS lcl_worker DEFINITION FINAL CREATE PUBLIC.
PUBLIC SECTION.
METHODS: constructor IMPORTING iv_input TYPE ty_result
io_output TYPE REF TO ty_result_tab,
run.
PRIVATE SECTION.
DATA: mv_input TYPE ty_result,
mo_output TYPE REF TO ty_result_tab.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Class lcl_worker IMPLEMENTATION
*&---------------------------------------------------------------------*
CLASS lcl_worker IMPLEMENTATION.
METHOD constructor.
mv_input = iv_input.
mo_output = io_output.
ENDMETHOD.
METHOD run.
DATA(ls_result) = VALUE ty_result( value = mv_input-value ).
APPEND ls_result TO mo_output.
ENDMETHOD.
ENDCLASS.
阅读全文