PackagePackagetypeA library you install and call yourself.See every one →What this type is →independently maintainedindependently maintainedoriginPublished by one person or a small company outside SAP. Says nothing about quality; the readings say that.See every one →

zthread

A threading library for ABAP modelled on Java's Thread and Runnable interface: a runnable class is forked as a thread, joined singly or all at once, and returns a result or a callback.

Running several pieces of work in parallel and collecting their results otherwise needs its own coordination logic each time. Here, a class implements the zif_runnable interface with the work to fork, is handed to a new thread instance, and started; a thread's join method, or the static join_all, waits for one thread or all of them to finish. A result comes back either by implementing zif_runnable_result and casting the thread's result once it has been joined, or through a callback object with on_result and on_error methods, which has to still be referenced when the thread finishes.

Facts

Last pushLicenceMIT

Where it sits

Lives in
Provides
Parallel processingABAPParallel processingcapabilitySplitting one run's work across several work processes so that it finishes sooner, and collecting what each part returned before the run goes on. [Async processing](capability:async-processing) is the neighbouring concern: work deferred past the request with a guarantee that it eventually happens, where parallel processing is throughput inside a run that waits for its results.

Who is behind it

xinitrc86xinitrc86PersonMaintains this
In the atlas: maintains 2, contributes to 3
@xinitrc86
maintainerLars HvamPersonContributes to this
In the atlas: maintains 13, contributes to 92, wrote 1
@larshp
1 contributor

Before choosing this

Signals

ActivityQuietNo published releases; repository quiet for 37 months

It publishes no releases, and its repository has been quiet for at least 24 full months. This may be normal for stable software; check whether its current state still fits your use.

What it declares

Every artifact

Type
Package
Origin
Independently maintained

As a package

Where it runs
does not say
Distributed through
does not say

Connections1

README

quoted from xinitrc86/zthread under MIT, read
xinitrc86/zthread/README.mdOn GitHub

ZThread

Simple parallel execution via Threads implemented in ABAP, based on JAVA Thread and its Runnable interface plus callback capabilities. Check the blog post for more details. Check the ABAPDocs for API information.

data(someProcessing) = new zcl_some_processing( some_data ).
data(anotherProcessing) = new zcl_some_other( some_data ).

new zcl_thread( someProcessing )->start( ).
new zcl_thread( anotherProcessing )->start( ).

"waits for threads to finish"
zcl_thread=>join_all( ).

write: 'All done!'

You can check the ABAP Docs for info and the source for examples. Feel free to provide new examples or colaborate with this.

Usage

Implement the zif_runnable interface with the logic you want to fork (start in a new thread), give it to a thread and start:

class zcl_my_runnable...
   interfaces zif_runnable.
   
   method constructor.
     "store data to process
   endmethod.
   
   method zif_runnable~run.
     "...process data.	 
   endmethod.
  
 endclass.
 
 data(firstSplit) = new zcl_my_runnable( data_to_process_1 ).
 data(secondSplit) = new zcl_my_runnable( data_to_process_2 ).
 
 new zcl_thread( firstSplit )->start( ).
 new zcl_thread( secondSplit )->start( ).
Waiting for Threads to end

The static method join_all allows join (wait for threads to finish).

data(myThread) = new zcl_thread(aRunnable).
new zcl_thread(anotherRunnable)->start( ).
new zcl_thread(yetAnotherRunnable)->start( ).

myThread->start( ).
myThread->join( ). "waits for this specific thread" 

zcl_thread=>join_all( ). "waits for all threads to finish" 
Retrieving threads results

You can retrieve results back from Threads by implementing the zif_runnable_result interface.

class zcl_my_runnable definition.
	"here runnable is implementing both interfaces"
	"but the result can be a separate object too"
	interfaces: zif_runnable, zif_runnable_result.
	data myNumbers type standard table of p...
	data myTotal type p.
	
	method get_total.
	  rv_result = myTotal.
	endmethod.
	
	method zif_runnable~run.
	  "sums numbers into myTotal"
	  myTotal = reduce #( 
		init total = 0.
		for number in myNumbers
		next
			total = total + number
	  ).
          "This is possoble because me is implementing 
          "zif_runnable_result. Could be another object.
          ro_result = me.
	endmethod.
	
endclass.

data(myRunnable) = new zcl_my_runnable( value #( ( 10 ) ( 10 ) ( 10 ) ) ).
data(myThread) = new zcl_thread( myRunnable ).

myThread->start( ).
myThread->join( ).

"retrieves the result, callers must know its type"
data(myResult) = cast zcl_my_runnable( myThread->get_result( ) ).
data(myTotal) = myResult->get_total( ).

write: myTotal. "30"
Defining callbacks

To defined a callback, implement interface zif_thread_callback| and passe it along to the thread. For an callback object to be called back, it must still be referenced once the thread is finished.

class zcl_callback_example definition.
   interfaces zif_thread_callback.
   
   private section.
      data myTotal type p.
   
   method zif_thread_callback~on_result.
   	data(result) = cast zcl_my_result( io_result ).
	myTotal = myTotal + result->get_total( ).
   endmethod.
   
   method zif_thread_callback~on_error.
      	raise exception type zcx_myError
	  	exporting previous = io_error.
   endmethod.
   
   method sumAll.
   
     data(runnable1) = zcl_my_Runnable( value #( ( 10 ) ( 10 ) ( 10 ) ) ). "adds to 30"
	 data(runnable2) = zcl_my_Runnable( value #( ( 20 ) ( 20 ) ( 20 ) ) ). "adds to 60"
	 
	 new zcl_thread( io_runnable = runnable1, io_callback = me )->start( ).
	 new zcl_thread( io_runnable = runnable2, io_callback = me )->start( ).
	 
	 zcl_thread=>join_all( ).
	 write: myTotal. "sum 30 + 60 =  90 "
   
   endmethod.
   
endclass.

Help welcomed

Reviews, examples, downports. Feel free to contribute.

MIT, read
Parallel processing
  1. abapify/promisePromise and async/await implementation for ABAP
  2. ABAPParallelizationServiceFramework for running ABAP work units in parallel tasks
  3. COROUTINES4ABAPCoroutine framework for ABAP with awaitable results
  4. zconcurrency_apiParallel task library for ABAP over the SPTA framework
  5. RFC_TOOLSA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.

Details

Sources3
dotabapdiscovered herechecked
github.com/xinitrc86/zthreadtree, pushes, contributors, CIchecked
README.mdquoted, and what the entry declareschecked

Sources are graph entities: the same page may back several entries, and an unreachable source flags every row that rests on it.

Compatibility and provenance
discovered viadotabap
first seen in the atlas
ecosystem and domainabap, operations-and-runtime
abaplint
issues under its own abaplint configuration1,050
lines3,050
statements1,709
objects29
files63
analysed, main
abaplint version2.120.84
Contributors1

From the repository’s contributor list, .

History in the atlas1
  1. First seen.
For your agent
Ref
artifact:xinitrc86-zthread
JSON
/artifacts/xinitrc86-zthread.json
MCP
Connecting an agent