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
Where it sits
Who is behind it
In the atlas: maintains 2, contributes to 3@xinitrc86maintainerLars HvamContributes to this
In the atlas: maintains 13, contributes to 92, wrote 1@larshp1 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
Connections
Splitting one run's work across several work processes
Download
README
quoted from xinitrc86/zthread under MIT, readZThread
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.
Others providing
Compare all 6 on the Parallel processing page →- abapify/promiseabapify/promiseA Promise and async/await implementation for ABAP, modelled on JavaScript's Promise API, that runs a resolver as a background task and offers all, race, resolve, reject and all_settled.independent, abapPromise and async/await implementation for ABAP
- ABAPParallelizationServiceABAPParallelizationServiceA generic framework, called from code, for running a function unit, report or method across many parameter combinations in parallel dialog or batch tasks.independent, abapFramework for running ABAP work units in parallel tasks
- COROUTINES4ABAPCOROUTINES4ABAPA coroutine framework for ABAP that launches a piece of work, such as a database query or a background task, as a task the caller awaits later while carrying on with other work.independent, abapCoroutine framework for ABAP with awaitable results
- zconcurrency_apizconcurrency_apiA Java-inspired ABAP library that runs tasks in parallel over SAP's SPTA framework without an RFC-enabled function module, capping concurrent tasks at 40% of the dialog work processes.independent, abapParallel task library for ABAP over the SPTA framework
- RFC_TOOLSRFC_TOOLSA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.independent, abapA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.
Details
Sources
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
abaplint
Contributors
History in the atlas
- First seen.
For your agent
- Ref
artifact:xinitrc86-zthread- JSON
- /artifacts/xinitrc86-zthread.json
- MCP
- Connecting an agent