首页 > 其他分享 >ABAP 多线程

ABAP 多线程

时间:2023-05-29 11:25:13浏览次数:44  
标签:gv sy package lv ABAP wp 多线程 TYPE

Code listing for: ZTEST9010
Description: 12345
*&---------------------------------------------------------------------*
*& Report ZTEST_ASYNC_RFC
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_async_rfc.

**global type, data
*TYPES: BEGIN OF gty_s_key_flight,
*         carrid TYPE sflight-carrid,
*         connid TYPE sflight-connid,
*         fldate TYPE sflight-fldate,
*       END OF gty_s_key_flight.
*TYPES: gty_t_key_flight TYPE TABLE OF gty_s_key_flight.
TYPES: BEGIN OF gty_s_flight_package,
         task_name TYPE char10,
         keys      TYPE zflight_t_key,
       END OF gty_s_flight_package.

DATA: gt_package TYPE TABLE OF gty_s_flight_package,
      gt_results    TYPE TABLE OF sflight,
      gv_snd_jobs   TYPE i,
      gv_rcv_jobs   TYPE i,
      gv_pp_running TYPE i.

**selection screen
SELECTION-SCREEN BEGIN OF BLOCK svr_group WITH FRAME TITLE title.
  PARAMETERS:
    p_group  TYPE rzllitab-classname DEFAULT 'parallel_generators' MODIF ID svr,
    p_parpro TYPE int1               DEFAULT 3 MODIF ID svr,
    p_size   TYPE i                  DEFAULT 5 MODIF ID svr. " normally 100, 1000"
SELECTION-SCREEN END OF BLOCK svr_group.


INITIALIZATION.
  title = 'Settings for Parallel Processing'.

START-OF-SELECTION.

  "..build packages"
  PERFORM build_packages.

  "..parallel processing"
  PERFORM parallel_processing.

  "..display result"
  PERFORM display_result.

FORM build_packages.

  DATA: lv_cursor       TYPE cursor,
        lv_index        TYPE char4,
        lv_task_counter TYPE char8,
        lt_keys         TYPE  zflight_t_key,
        ls_package      TYPE gty_s_flight_package.

  OPEN CURSOR WITH HOLD @lv_cursor FOR
    SELECT carrid, connid, fldate
    FROM sflight
    WHERE carrid = 'AA' OR carrid = 'AZ'.

  DO.

    lv_index = sy-index.
    CONCATENATE 'TASK' lv_index INTO lv_task_counter.

    FETCH NEXT CURSOR @lv_cursor
    INTO TABLE @lt_keys
    PACKAGE SIZE @p_size.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.

    ls_package = VALUE #( task_name = lv_task_counter
                          keys      = lt_keys ).

    APPEND ls_package TO gt_package.

  ENDDO.

ENDFORM.

FORM parallel_processing.

  DATA: lv_wp_total      TYPE i,
        lv_wp_free       TYPE i,
        lv_curr_wp_total TYPE i,
        lv_curr_wp_free  TYPE i,
        lv_allow_wp      TYPE i.

  IF p_group IS NOT INITIAL.
    " Get the list of application server available and those are free (no of WP & free WP)"
    CALL FUNCTION 'SPBT_INITIALIZE'
      EXPORTING
        group_name                     = p_group
      IMPORTING
        max_pbt_wps                    = lv_wp_total
        free_pbt_wps                   = lv_wp_free
      EXCEPTIONS
        invalid_group_name             = 1
        internal_error                 = 2
        pbt_env_already_initialized    = 3
        currently_no_resources_avail   = 4
        no_pbt_resources_found         = 5
        cant_init_different_pbt_groups = 6
        OTHERS                         = 7.
    IF sy-subrc <> 0 AND sy-subrc <> 3.
      MESSAGE 'No resources available.' TYPE 'E'.
      EXIT.
    ENDIF.
  ENDIF.

  LOOP AT gt_package INTO DATA(ls_package).
    " generating jobs"
    gv_snd_jobs = gv_snd_jobs + 1.
    gv_pp_running = gv_pp_running + 1.

    IF p_group IS NOT INITIAL.

      DO.
        " get the current available workprocess(time-dependent)"
        CALL FUNCTION 'SPBT_GET_CURR_RESOURCE_INFO'
          IMPORTING
            max_pbt_wps                 = lv_curr_wp_total
            free_pbt_wps                = lv_curr_wp_free
          EXCEPTIONS
            internal_error              = 1
            pbt_env_not_initialized_yet = 2
            OTHERS                      = 3.

        IF p_parpro > 0.
          lv_allow_wp = p_parpro.
        ELSE.
          "if maximum number of processes is not restricted, take only half of the free processes"
          lv_allow_wp = lv_curr_wp_free / 2.
        ENDIF.

        " if current free wp is available with more than 2 wp"
        " and the current running is less than allowed wp,"
        " then there is enough wp for work of parallelization"
        IF lv_curr_wp_free GT 2 AND gv_pp_running LT lv_allow_wp.
          call function 'ZTEST_FLIGHT_GETDETAIL'
            STARTING NEW TASK ls_package-task_name
            DESTINATION IN GROUP p_group
            PERFORMING call_back ON END OF TASK
            EXPORTING
              it_keys               = ls_package-keys
            EXCEPTIONS
              communication_failure = 1
              system_failure        = 2
              resource_failure      = 3.
          IF sy-subrc <> 0.
            MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          ELSE.
            EXIT.
          ENDIF.

        ELSE. " otherwise, it needs to wait until the wp are available"
          WAIT UP TO 1 SECONDS.
          CONTINUE.
        ENDIF.
      ENDDO.

    ELSE.
      "if no server group has been selected, then parallel processing use the Default Group"
      call function 'ZTEST_FLIGHT_GETDETAIL'
        STARTING NEW TASK ls_package-task_name
        DESTINATION IN GROUP DEFAULT
        PERFORMING call_back ON END OF TASK
        EXPORTING
          it_keys               = ls_package-keys
        EXCEPTIONS
          communication_failure = 1
          system_failure        = 2
          resource_failure      = 3.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        EXIT.
      ENDIF.

    ENDIF.

  ENDLOOP.

  " wait, until all the jobs were received."
  WAIT UNTIL gv_rcv_jobs >= gv_snd_jobs.

ENDFORM.

FORM call_back USING p_task TYPE clike.

  DATA: lt_results TYPE STANDARD TABLE OF sflight.

  " reveived jobs"
  gv_rcv_jobs = gv_rcv_jobs + 1.

  " retrieve results from parallelization"
  RECEIVE RESULTS FROM FUNCTION 'ZTEST_FLIGHT_GETDETAIL'
    IMPORTING et_results = lt_results.
  IF sy-subrc = 0.
    " running jobs"
    gv_pp_running = gv_pp_running - 1.
  ENDIF.

  APPEND LINES OF lt_results TO gt_results.

ENDFORM.

FORM display_result.

  DATA: lo_alv TYPE REF TO cl_salv_table.

  CALL METHOD cl_salv_table=>factory
    IMPORTING
      r_salv_table = lo_alv
    CHANGING
      t_table      = gt_results.

  lo_alv->display( ).

ENDFORM.


*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
*   No resources available.
Extracted by Mass Download version 1.5.5 - E.G.Mellodew. 1998-2022. Sap Release 756

 

标签:gv,sy,package,lv,ABAP,wp,多线程,TYPE
From: https://www.cnblogs.com/ZJY-1314/p/17439902.html

相关文章

  • ABAP 增强查找工具
    *&---------------------------------------------------------------------**&ReportZ_BARRY_FIND_EXIT_BADI*&---------------------------------------------------------------------**&*&---------------------------------------------------......
  • ABAP 用户批量导入
    REPORTZ_USER_IMPORT.TABLES:E070.DATA:BDCDATALIKEBDCDATAOCCURS0WITHHEADERLINE.DATA:MESSTABLIKEBDCMSGCOLLOCCURS0WITHHEADERLINE.DATA:P_SNTYPEI.****DATA:W_DOCIDTYPEDSVASDOCID,W_EXTENSIONTYPEDSVASDOCID,W......
  • ABAP 用户权限查询报表
    *&---------------------------------------------------------------------**&REPORTZUSER*&---------------------------------------------------------------------**&*&--------------------------------------------------------------------......
  • ABAP 用户角色导入
    *&---------------------------------------------------------------------**&ReportZ_USER_ROLES_IMPORT*&---------------------------------------------------------------------**&*&------------------------------------------------------......
  • ABAP 通用自建表批到
    *&---------------------------------------------------------------------**&ReportZUPLOAD_TABLE*&---------------------------------------------------------------------**&*&------------------------------------------------------------......
  • java多线程
    java多线程进程、线程与多线程进程是执行程序的一次执行过程,是一个动态的概念,是系统支援分配的单位通常一个进程可以包含一个或多个线程。线程是CPU调度和执行的单位线程就是独立执行的路径,由cpu调度线程会带来额外的开销,如cpu调度时间,并发控制开销每个线程在自己......
  • python3多线程
    1、线程和进程计算机资源,指的是cpu计算资源、内存资源、接口读写资源等。进程:计算机中用于分配资源的最小个体。不同进程之间并不共享内存。线程:是进程内部的资源分配对象,一个进程最少会有一个线程。同一进程下的不同线程可以共享内存。2、并行和并发并行:同一时间段,交替前行。并......
  • ABAP-屏幕设计-上门拜访动态切换
    *&---------------------------------------------------------------------**&ReportZHQ_01_04*&*&---------------------------------------------------------------------**&*&*&-------------------------------------------------......
  • C++多线程
    多线程:thread_local每个线程都会有自己的一份数据,不会共享数据,避免竞争 volatile告诉编译器每次取数据都要从内存里取 使用情况:信号处理内存映射相关非本地跳转 std::atomic定义操作为原子操作,不可被线程分开 接口  std::shreadC++的线程类,但是不保证......
  • Java大文件分片上传/多线程上传方案
    ​ 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。先说下要求:PC端全平台支持,要求支持Windows,Mac,Linux支持所有浏览器。支持文件批量上传支持文件夹上传,且要求在服务端保留层级结构。文件夹数量要求支持到10W......